sorting by multiple criterias (sub-sorting)

Discussion in 'Perl Misc' started by Tom Kirchner, Oct 11, 2003.

  1. Tom Kirchner

    Tom Kirchner Guest

    hello!

    Im currently writing on a part of a program where there is an
    array of hash-refs holding data. and I want to sort these
    array-elements (the hash-refs) by a first criteria and then
    another one. E.g. If each hash would be something like:

    @array = (
    { 'name' => 'dan', 'age' => '12', 'shoesize' => '34' },
    { 'name' => 'tom', 'age' => '19', 'shoesize' => '56' },
    { 'name' => 'tom', 'age' => '23', 'shoesize' => '43' },
    { 'name' => 'frank', 'age' => '42', 'shoesize' => '24' },
    # ...
    );

    what I want is: sorting first by 'name' and then, when there
    are multiple records with the same name, sorting this (sub)group
    by their 'age' and if there are records with the same
    name and same age, sorting these by their shoesize.
    This is just an example and the real thing is much more
    complex, but I need a general idea of how this could be done.

    thanks
    tom
     
    Tom Kirchner, Oct 11, 2003
    #1
    1. Advertising

  2. 9:28am, IP packets from Tom Kirchner delivered:

    > hello!
    >
    > Im currently writing on a part of a program where there is an
    > array of hash-refs holding data. and I want to sort these
    > array-elements (the hash-refs) by a first criteria and then
    > another one. E.g. If each hash would be something like:
    >
    > @array = (
    > { 'name' => 'dan', 'age' => '12', 'shoesize' => '34' },
    > { 'name' => 'tom', 'age' => '19', 'shoesize' => '56' },
    > { 'name' => 'tom', 'age' => '23', 'shoesize' => '43' },
    > { 'name' => 'frank', 'age' => '42', 'shoesize' => '24' },
    > # ...
    > );


    sub sort_func {
    $a->{name} cmp $b->{name} ||
    $a->{age} <=> $b->{age} ||
    $a->{shoesize} <=> $b->{shoesize}
    }

    @sorted_array = sort sort_func (@array);


    >
    > what I want is: sorting first by 'name' and then, when there
    > are multiple records with the same name, sorting this (sub)group
    > by their 'age' and if there are records with the same
    > name and same age, sorting these by their shoesize.
    > This is just an example and the real thing is much more
    > complex, but I need a general idea of how this could be done.
    >
    > thanks
    > tom
    >


    --
    echo | perl -pe 'y/A-Za-z/N-ZA-Mn-za-m/'
    T h i s t a g l i n e h a s b e e n u n z i p p e d .
     
    Cognition Peon, Oct 11, 2003
    #2
    1. Advertising

  3. Tom Kirchner <> wrote:
    > Im currently writing on a part of a program where there is an
    > array of hash-refs holding data. and I want to sort these
    > array-elements (the hash-refs) by a first criteria and then
    > another one.


    There are two FAQs dealing with this topic:

    % perldoc -q sort

    > E.g. If each hash would be something like:
    >
    > @array = (
    > { 'name' => 'dan', 'age' => '12', 'shoesize' => '34' },


    Just a style nit:

    { name => 'dan', age => 12, shoesize => 34 },

    > { 'name' => 'tom', 'age' => '19', 'shoesize' => '56' },
    > { 'name' => 'tom', 'age' => '23', 'shoesize' => '43' },
    > { 'name' => 'frank', 'age' => '42', 'shoesize' => '24' },
    > # ...
    > );
    >
    > what I want is: sorting first by 'name' and then, when there
    > are multiple records with the same name, sorting this (sub)group
    > by their 'age' and if there are records with the same
    > name and same age, sorting these by their shoesize.


    Quoting from "How do I sort an array by (anything)?"

    If you need to sort on several fields, the following paradigm
    is useful.

    @sorted = sort { field1($a) <=> field1($b) ||
    field2($a) cmp field2($b) ||
    field3($a) cmp field3($b)
    } @data;

    This works because when two elements of the input list are
    equal, the <=> and cmp operators will return zero (and the
    sub will fall through to the next comparison).

    To adapt it to your example, just substitute hash lookups for
    the subroutine call.

    @sorted = sort { $a->{name} cmp $b->{name} ||
    $a->{age} <=> $b->{age} ...

    --
    Steve
     
    Steve Grazzini, Oct 11, 2003
    #3
  4. In article
    <>,
    Cognition Peon <> wrote:

    > 9:28am, IP packets from Tom Kirchner delivered:
    >
    > > hello!
    > >
    > > Im currently writing on a part of a program where there is an
    > > array of hash-refs holding data. and I want to sort these
    > > array-elements (the hash-refs) by a first criteria and then
    > > another one. E.g. If each hash would be something like:
    > >
    > > @array = (
    > > { 'name' => 'dan', 'age' => '12', 'shoesize' => '34' },
    > > { 'name' => 'tom', 'age' => '19', 'shoesize' => '56' },
    > > { 'name' => 'tom', 'age' => '23', 'shoesize' => '43' },
    > > { 'name' => 'frank', 'age' => '42', 'shoesize' => '24' },
    > > # ...
    > > );
    > >
    > > what I want is: sorting first by 'name' and then, when there
    > > are multiple records with the same name, sorting this (sub)group
    > > by their 'age' and if there are records with the same
    > > name and same age, sorting these by their shoesize.
    > > This is just an example and the real thing is much more
    > > complex, but I need a general idea of how this could be done.

    >
    > sub sort_func {
    > $a->{name} cmp $b->{name} ||
    > $a->{age} <=> $b->{age} ||
    > $a->{shoesize} <=> $b->{shoesize}
    > }
    >
    > @sorted_array = sort sort_func (@array);


    of course, the o.p. does not _have_ to use a subroutine:

    @sorted_array = sort {
    $a->{name} cmp $b->{name} ||
    $a->{age} <=> $b->{age} ||
    $a->{shoesize} <=> $b->{shoesize}
    } @array;

    --
    Michael Budash
     
    Michael Budash, Oct 11, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. THY
    Replies:
    1
    Views:
    411
    Steve C. Orr, MCSD
    Aug 19, 2003
  2. Kathy Burke
    Replies:
    3
    Views:
    417
    Kathy Burke
    Nov 24, 2003
  3. Ben
    Replies:
    2
    Views:
    954
  4. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,165
    Lawrence D'Oliveiro
    May 20, 2011
  5. Harkonnen

    How to securize a webservice with several criterias ?

    Harkonnen, Oct 14, 2008, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    136
    Harkonnen
    Oct 14, 2008
Loading...

Share This Page