sathiya.sw has asked for the wisdom of the Perl Monks concerning the following question:

I need to sort an array based up on the partial data of it ?
my @array = ("0x4b82_5", "0x4b82_58","0x4b82_651", "0x4b82_551");
Output: 0x4b82_5 0x4b82_58 0x4b82_551 0x4b82_651
Sort should happen based up on the value after the _ in the data.
Sathiyamoorthy

Replies are listed 'Best First'.
Re: sort based upon the partial data
by moritz (Cardinal) on Mar 03, 2009 at 09:54 UTC
    sort can do custom comparisons, so you to extract everything after the _ (either with regexes or with split) and compare them.
Re: sort based upon the partial data
by GrandFather (Saint) on Mar 03, 2009 at 10:11 UTC

    A common technique for solving this sort of problem is to transform the data into an easily sorted form, then transform the sorted data back into the original form. The Schwartzian transform is probably the most common for of this technique. See the replies to What is "Schwarzian Transform" (aka Schwartzian) for a discussion of it and mention of the related Orcish Maneuver.


    True laziness is hard work
Re: sort based upon the partial data
by Corion (Patriarch) on Mar 03, 2009 at 09:55 UTC

    Just to clear up a potential misunderstanding, we don't write code here for you.

    You can help us to help you better if you show what code you already have, example data, and if you describe how it fails to do what you want, and what you've tried already to fix this.

Re: sort based upon the partial data
by nagalenoj (Friar) on Mar 03, 2009 at 10:06 UTC
    #!/usr/bin/perl use warnings; sub func { $a =~ s/.*_(\d+)$/$1/; $b =~ s/.*_(\d+)$/$1/; $a <=> $b; } my @array = ("0x4b82_555","0x4b82_5", "0x4b82_58","0x4b82_651", "0x4b8 +2_551"); my @result = sort func @array; print "@result";

    The above code will give the result as you want.

      That technique is called "destructive sort" :-P