Help - Search - Member List - Calendar
Full Version: sorting list of array
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Beast
I have an array:


my @employee = ( [29243, 'john', 'John doe'],
[24322, 'jane', 'Jane doe'],
[27282, 'james', 'James doe']
);


Is there any builtin function in perl to sort the above array based on
uid, username or fulname?


--

--beast

Jeff 'japhy' Pinyan
On Jul 13, Beast said:

QUOTE
my @employee = ( [29243, 'john', 'John doe'],
[24322, 'jane', 'Jane doe'],
[27282, 'james', 'James doe']
);

Is there any builtin function in perl to sort the above array based on uid,
username or fulname?

There is a built-in function to sort a list, yes. But the mechanism by
which to sort the list is, in this case, up to you to provide. This
works:

my @sorted = sort {
$a->[0] <=> $b->[0]
} @employees;

In the sort() block, $a and $b are two elements of the list being
compared. Since the elements are array references, and their first
element is their UID, I've used $a->[0] <=> $b->[0] to compare those two
values. See the documentation for 'sort' (perldoc -f sort) for more
details.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart

Jeff 'japhy' Pinyan
On Jul 13, Beast said:

QUOTE
Jeff 'japhy' Pinyan wrote:

Is there any builtin function in perl to sort the above array based on uid,
username or fulname?

There is a built-in function to sort a list, yes.  But the mechanism by
which to sort the list is, in this case, up to you to provide.  This works:

my @sorted = sort {
$a->[0] <=> $b->[0]
} @employees;


However, is this scalable if, for example list is more than 5000 ?

Sure, it's scalable... I believe perl uses a mergesort nowadays, which is
better on larger lists anyway. Regardless, given the data you showed us,
this is the simplest and shortest (and most likely fastest) way to sort it
based on a given index.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart

Jeff 'japhy' Pinyan
On Jul 13, Ankur Gupta said:

QUOTE
Jeff 'japhy' Pinyan wrote:

Is there any builtin function in perl to sort the above array based
on uid, username or fulname?

my @sorted = sort {
$a->[0] <=> $b->[0]
} @employees;

Hey, This will sort only numbers. Will have no effect if the values have
text.

You're right, but I would expect -- after my explanation of how it works,
and my pointer to the sort() docs -- that the OP would be able to
extrapolate the rest.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart

Beast
Jeff 'japhy' Pinyan wrote:
QUOTE

Is there any builtin function in perl to sort the above array based on
uid, username or fulname?


There is a built-in function to sort a list, yes.  But the mechanism by
which to sort the list is, in this case, up to you to provide.  This works:

my @sorted = sort {
$a->[0] <=> $b->[0]
} @employees;


Thank you.
However, is this scalable if, for example list is more than 5000 ?
(ie. obtain data from ldap which we can not ask data provider to sort
the entry for us)

--

--beast

Ankur Gupta
Beast <mailto:[Email Removed]> wrote:
QUOTE
I have an array:


my @employee = ( [29243, 'john', 'John doe'],
[24322, 'jane', 'Jane doe'],
[27282, 'james', 'James doe']
);


Is there any builtin function in perl to sort the above array based
on uid, username or fulname?

perldoc -f sort.

Something like this should work.

__BEGIN__
use strict;
use warnings;

my @employee = ( [29243, 'john', 'John doe'],
[24322, 'jane', 'Jane doe'],
[27282, 'james', 'James doe']
);

# Specify the column on what you want to search as a argument..
my $i = $ARGV[0] || 0;

# sort subroutine..
my $subsort = sub {
#no warnings;
$a->[$i] <=> $b->[$i]
||
$a->[$i] cmp $b->[$i]
};

print "Sorting by column $in";
foreach my $j ( sort $subsort @employee ){
print "@$jn";
}
__END__

Some sample runs gave me the following result...

$-> test-perl.pl 0
Sorting by column 0
24322 jane Jane doe
27282 james James doe
29243 john John doe

$-> test-perl.pl 1
Sorting by column 1
27282 james James doe
24322 jane Jane doe
29243 john John doe

$-> test-perl.pl 2
Sorting by column 2
27282 james James doe
24322 jane Jane doe
29243 john John doe

There can be a better way to do it...

HTH....

Note: Will give warnings if warnings is enabled for columns which have
text.

--Ankur

Bad style destroys an otherwise superb program.

Ankur Gupta
Jeff 'japhy' Pinyan <mailto:[Email Removed]> wrote:
QUOTE
On Jul 13, Beast said:

Jeff 'japhy' Pinyan wrote:

Is there any builtin function in perl to sort the above array based
on uid, username or fulname?

There is a built-in function to sort a list, yes.  But the mechanism
by which to sort the list is, in this case, up to you to provide.
This works:

my @sorted = sort {
$a->[0] <=> $b->[0]
} @employees;



Hey, This will sort only numbers. Will have no effect if the values have
text.

QUOTE
However, is this scalable if, for example list is more than 5000 ?

Sure, it's scalable... I believe perl uses a mergesort nowadays,
which is better on larger lists anyway.  Regardless, given the data
you showed us, this is the simplest and shortest (and most likely
fastest) way to sort it based on a given index.

For more information...

[...]

use sort 'stable'; # guarantee stability
use sort '_quicksort'; # use a quicksort algorithm
use sort '_mergesort'; # use a mergesort algorithm
use sort 'defaults'; # revert to default behavior
no sort 'stable'; # stability not important

use sort '_qsort'; # alias for quicksort

my $current = sort::current(); # identify prevailing algorithm

[...]

--Ankur

Bad style destroys an otherwise superb program.


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2005 Invision Power Services, Inc.