Home > Archive > PERL Beginners > February 2006 > Sort
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| iavian 2006-02-21, 3:55 am |
| Hi All
I got an 3 dimensional array , i want to sort them on 3 , 2 , 1st
dimension ..
Regards
Vijay
| |
| usenet@DavidFilmer.com 2006-02-21, 3:55 am |
| iavian wrote:
> Hi All
>
> I got an 3 dimensional array , i want to sort them on 3 , 2 , 1st dimension ..
Ah, my friend, what you want is the famous Schwartzian Transform (named
after Randal, of course).
See here: http://tinyurl.com/sx66o for links and references to this
technique.
--
http://DavidFilmer.com
| |
| usenet@DavidFilmer.com 2006-02-21, 3:55 am |
| use...@DavidFilmer.com wrote:
> Ah, my friend, what you want is the famous Schwartzian Transform
I should have mentioned - you need to be careful whether you're doing
numeric or alphabetic comparisons of the various indexes. For example:
print map { $_->[0] } #Randal Schwartz Rocks!!!
sort { $a->[3] <=> $b->[3] #numerically sort third index
|| $a->[2] <=> $b->[2] #numerically sort second index
|| $a->[1] cmp $b->[1] #alphabetic sort first index
} @array;
Be careful to note the difference between <=> and cmp and use whichever
is appropriate to your data.
--
http://DavidFilmer.com
| |
| Uri Guttman 2006-02-21, 3:55 am |
| >>>>> "u" == usenet <usenet@DavidFilmer.com> writes:
u> use...@DavidFilmer.com wrote:[color=darkred]
u> I should have mentioned - you need to be careful whether you're doing
u> numeric or alphabetic comparisons of the various indexes. For example:
u> print map { $_->[0] } #Randal Schwartz Rocks!!!
u> sort { $a->[3] <=> $b->[3] #numerically sort third index
u> || $a->[2] <=> $b->[2] #numerically sort second index
u> || $a->[1] cmp $b->[1] #alphabetic sort first index
u> } @array;
and to eliminate that key comparison confusion and speed things up if
you have large data sets, check out Sort::Maker on cpan. there is also
an article on it in the module and it can still be found on perl.com.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
| |
| iavian 2006-02-21, 3:55 am |
| , thanks buddy
|
|
|
|
|