Jeff 'japhy' Pinyan <mailto:japhy (AT) perlmonk (DOT) orgwrote:
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.
>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
[]
Bad style destroys an otherwise superb program.
Jul 13, Ankur Gupta said:
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 P would be able to
extrapolate the rest.
Copyright © 2008 www.emsdn.com • All rights reserved • CMS Theme by www.emsdn.com - 0.344