Sorting

Discussion in 'Perl Misc' started by Mahesh, Nov 29, 2004.

  1. Mahesh

    Mahesh Guest

    I had a two dimensional array.

    Suppose it is a student array where student[0] represents the student
    name and student[1] represents the date-of-birth of student.

    I want to sort the student array based on student[0] column so that
    the association between the name and dateofbirth remains same...

    Is there any way that I can sort a multi dimensional array based on
    some column.

    Please help me in this issue..

    Thanks in Advance,
    Mahesh
    Mahesh, Nov 29, 2004
    #1
    1. Advertising

  2. Mahesh wrote:

    > I had a two dimensional array.
    >
    > Suppose it is a student array where student[0] represents the student
    > name and student[1] represents the date-of-birth of student.


    It might be better to represent each student record as a hash, resulting
    in an array of hashes instead of an array of arrays - but that's a
    different question.

    > I want to sort the student array based on student[0] column so that
    > the association between the name and dateofbirth remains same...


    Okay, let's start with a two-dimensional array with a few students:

    my $list_of_students = [
    [ 'Charlie Brown', '1995-07-01' ],
    [ 'Peppermint Pattie', '1995-03-10' ],
    [ 'Linus', '1995-08-20' ],
    ];

    The above is a two-dimensional array, or "list of lists" in Perl
    parlance. See 'perldoc perllol' for details.

    > Is there any way that I can sort a multi dimensional array based on
    > some column.


    Easy, just supply a comparison function to Perl's built-in sort() function:

    my @sorted_students = sort { $a->[1] cmp $b->[1] } @$list_of_students;

    > Please help me in this issue..


    This is just a quick example - for a full discussion see:

    perldoc -q sort
    perldoc -f sort

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Nov 29, 2004
    #2
    1. Advertising

  3. Mahesh

    Bill Smith Guest

    "Mahesh" <> wrote in message
    news:...
    > I had a two dimensional array.
    >
    > Suppose it is a student array where student[0] represents the student
    > name and student[1] represents the date-of-birth of student.
    >
    > I want to sort the student array based on student[0] column so that
    > the association between the name and dateofbirth remains same...
    >
    > Is there any way that I can sort a multi dimensional array based on
    > some column.


    Strictly speaking, perl does not support multi-dimensional arrays. The
    optional syntax for indexes largely frees us from this constraint. In
    perl, the structure that you describe is implemented as an array of
    references, each of which refers to a two element anonymous array which
    contains a student name and corresponding birthday. Sorting is one of
    the situations where the special syntax does not help much.

    In terms of the implementation, you want to sort the array of
    references. The data arrays should not be changed at all. Sherm
    already provided the code to do this.

    I hope this insight helps you understand the references he provided.

    Bill
    Bill Smith, Nov 29, 2004
    #3
    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. Ashish

    sorting techniques

    Ashish, Nov 15, 2003, in forum: VHDL
    Replies:
    0
    Views:
    547
    Ashish
    Nov 15, 2003
  2. Replies:
    2
    Views:
    1,427
    James Kanze
    Jul 6, 2010
  3. Jason
    Replies:
    0
    Views:
    384
    Jason
    Oct 4, 2006
  4. Tom Kirchner

    sorting by multiple criterias (sub-sorting)

    Tom Kirchner, Oct 11, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    474
    Michael Budash
    Oct 11, 2003
  5. Íéêüëáïò Êïýñáò

    Sorting a set works, sorting a dictionary fails ?

    Íéêüëáïò Êïýñáò, Jun 10, 2013, in forum: Python
    Replies:
    12
    Views:
    157
    Ulrich Eckhardt
    Jun 10, 2013
Loading...

Share This Page