|XP is just a number|
Re: Perl Sorting One Linerby kcott (Chancellor)
|on May 07, 2013 at 11:12 UTC||Need Help??|
Welcome to the monastery.
Here's an example showing various techniques used in sorting columns:
Here, FIRST column is sorted into descending alphabetical order; where items are the same, SECOND column is sorted into ascending alphabetical order; where items are the same, THIRD column is sorted into descending numerical order.
Decide whether sorting will be applied to more than one column and, if so, what order to use. For instance, to sort by last name and then, if last names are the same, sort by first name, you might use something like:
Look at the type of data you're sorting and use the appropriate operator: cmp for strings and <=> for numbers.
For ascending sorts (0 -> 9, A -> Z, etc.) compare $a data to $b data (e.g. $a->[SECOND] cmp $b->[SECOND]); for descending sorts, reverse the $a and $b values (e.g. $b->[THIRD] <=> $a->[THIRD]).
I've implemented the example as a one-liner (albeit with some formatting for readability purposes). So, for a one-off, throw-away command line, a one-liner is fine; however, save the code to a script if you're ever going to: run this again; need a record of what code you used; want to refer back to it to refresh your memory on how to do this.