in reply to Re^4: Custom Sort An AoA
in thread Custom Sort An AoA
Ah yes, this is along the lines of what I was looking for.
Although it feels a bit strange spoon-feeding you a solution... ;)
#!/usr/bin/perl -l sub LRsort { @$a <=> @$b || do { for (1..@$a) { my $LR = $a->[-$_] =~ /^\d+(?:\.\d+)?$/ && $b->[-$_] =~ /^\d+(?:\.\d+)?$/ ? $a->[-$_] <=> $b->[-$_] : $a->[-$_] cmp $b->[-$_]; return $LR if $LR; } } } sub d { print '[',join(', ',map{"'$_'"}@$_),']' for @_ } my @list = ( ['blah', 'asdf', 'foo', 'bar'], ['two'], ['zzz', 'def', 'ghi'], ['one'], ['mmm', 'def', 'ghi'], ['qqq', 'xyz', 'aaa'], ); my @sorted = sort LRsort @list; print "strings sorted"; d @sorted; $_ = [ map { my $o; $o+= ord for split//;$o } @$_ ] for @list; print "strings numified"; d @list; my @sorted = sort LRsort @list; print "numeric"; d @sorted; _END_ strings sorted ['one'] ['two'] ['qqq', 'xyz', 'aaa'] ['mmm', 'def', 'ghi'] ['zzz', 'def', 'ghi'] ['blah', 'asdf', 'foo', 'bar'] strings numified ['407', '414', '324', '309'] ['346'] ['366', '303', '312'] ['322'] ['327', '303', '312'] ['339', '363', '291'] numeric ['322'] ['346'] ['339', '363', '291'] ['327', '303', '312'] ['366', '303', '312'] ['407', '414', '324', '309']
Implementing the orcish maneuver as kennethk suggested is left as an excercise for the reader.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Custom Sort An AoA
by Limbic~Region (Chancellor) on Apr 02, 2014 at 12:07 UTC |