Sorting by key or by comparator - Perl 6
Archive - Originally posted on "The Horse's Mouth" - 2016-01-03 11:18:58 - Graham EllisYou can sort lists / arrays in Perl 6 - but unlike previous Perl versions, you only need to give a single key expression in your snippet of sort code, rather than instructions on how to compare two values. Of course, since Perl is still eclectic, the old way remains supported too!
Here is an example in which I sorted a list, replacing the previous list. Each item being sorted is itself a list, and I've sorted by the fifth element (element no. 4) of those internal lists.
@opposite = @opposite.sort({@$^a[4]});
Complete code - [here]. Of course, since Perl never uses 3 characters where 2 will do, my example may be awful hard to follow at first ... please ask me about Perl 6 training courses, currently under development - happy to help with bespoke training requirements from February 2016.
Note - in the move from Python 2 to Python 3, sorting has changed in that language too - also moving from a comparator to a key approach - so it's something we're very familiar with when we come to teach you!