Perl

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • sorting list of array

    1 answers - 1515 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    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.
  • No.1 | | 525 bytes | |

    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.

Re: sorting list of array


max 4000 letters.
Your nickname that display:
In order to stop the spam: 1 + 0 =
QUESTION ON "Perl"

EMSDN.COM