Sorting

M

Mahesh

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
 
S

Sherm Pendley

Mahesh said:
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--
 
B

Bill Smith

Mahesh said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top