Multi-criteria sorting in Perl
This is so simple I want to cry.
You know when you need to sort a list following several critiria? “Sort the files by type, then sort those that have the same type by name, then by size”.
Perl’s //sort// and comparison operators (//cmp// and //<=>//) come in handy:
@sorted = sort { $a->type cmp $b->type or $a->name cmp $b->name or $a->size <=> $b->size} @list;
Assuming of course the objects in //@list// have //type//, //name// and //size// methods available.
This reads litteraly as //”compare //a// and //b//’s types, and if they’re the same then compare their names, and then compare their sizes”//.
Of course you should never compare file sizes, because size doesn’t matter.