in reply to Sorting by column
You're most of the way there. cmp returns 0 if the values are equal. All you need to do in your sort routine is assign the value of your initial cmp to an intermediate variable. If that variable is not zero, then you return that value. If that variable is zero, then you do a second compare of the 3rd item ( 2nd index ) of each row, and use that as the result.
As an alternative ( and in this case probably easier ) solution, you could also just compare the 2 right hand columns as strings. Try using split with the limit option.
sort { my(undef,$a_val) = split(/\s+/,$a,2); my(undef,$b_val) = split(/\s+/,$b,2); $a_val cmp $b_val; }
|
|---|