If the end goal is to put the files in the archive in the desired order, then a simple solution is to create an archive with the files to sort first, then add the files to sort last to the existing archive. This can of course be generalized to more than two sort segments. This requires creating an archive file, you can't append to an archive in a pipeline.
tar cf foo.tar /images/*collage.*
find /images ! -name '*collage.*' -print0 | sort -z | tar rf foo.tar --null -T -
If you want to perform a custom sort with just basic utilities, one possible method is to add a prefix to the line indicating which group the item is a part of. Arrange for the prefix tags to be sorted in the order you want the groups to be sorted, and strip those prefixes afterwards.
find /images -print0 |
sed -z -e 's/.*collage\./1&/' -e 't' -e 's/^/2/' |
sort -z |
sed -z 's/^.//' |
tar …
Another approach would be to do the sorting in another language such as Perl, Python or Ruby where you can express a custom sort. If you do that, you could do the filename gathering and even the archive production in that language. Here's a Perl example that just does the sorting:
perl -e '$,=$\="\0"; print sort {
$a =~ /collage\./ ? $b =~ /collage\./ ? $a cmp $b : 1 :
$b =~ /collage\./ ? -1 : $a cmp $b
} @ARGV' /images/* | tar …
And another example that does the sorting via a temporary rewrite (it's called “Schwarztian transform” in the Perl community):
perl -e '$,=$\="\0"; print
map {substr($_,1)}
sort
map {$_ = (/collage\./ ? "1" : "2") . $_}
@ARGV' /images/* | tar …
Both of these examples will fail if the combined length of the file names exceeds the command line length limit. To avoid that, let Perl generate the file names.
perl -e '$,=$\="\0"; print
map {substr($_,1)}
sort
map {$_ = (/collage\./ ? "1" : "2") . $_}
glob("/images/*")' /images/* | tar …
If you need to generate file names recursively or apply some filtering, you can use File::Find. If you want to generate the archive in Perl, you can use Archive::Tar.
(grep -Z collage tempfile; grep -Zv collage tempfile) | tar ...
– Jeff Schaller♦ Mar 6 '16 at 15:10