41 users online (0 members and 41 guests)  


  Results 1 to 3 of 3

Related

  1. Top 10 sorting    Forum: Javascript Forum
    Replies: 11
  2. Sorting by column on webpage    Forum: CGI Perl Forum
    Replies: 2
  3. Time sorting    Forum: Javascript Forum
    Replies: 1
  4. Sorting alhpanumeric files    Forum: CGI Perl Forum
    Replies: 1
  5. Replies: 1
  1. #1
    blink's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2005
    Posts
    19

    Sorting with perl

    Hello all,

    I have a problem with sorting. I have a flatfile database of obtained points and user's real name. I get the data and add them to an array in a string form as follows:

    @totals = (@totals, "$total_count|$real_name|");

    Then I sort the @totals like so:

    @totals_sorted = sort {$a <=> $b} @totals;

    When I print out the array I get it sorted as below:

    foreach my $line (@totals_sorted)
    {
    my ($score, $name) = split(/\|/, $line);
    print "$name $score\n";
    }

    B 5
    A 5
    C 4
    E 3
    D 3

    My problem is that I need to sort the array first by the point and then by the user's name:

    A 5
    B 5
    C 4
    D 3
    E 3

    How do I go about it?

  2. #2
    netroact's Avatar
    New User

    Status
    Offline
    Join Date
    Feb 2006
    Posts
    19

    Re: Sorting with perl

    Code:
     
    @totals = (@totals, "$total_count|$real_name|");
    Then I sort the @totals like so:
    @totals_sorted = sort {$a <=> $b} @totals;
    @totals_sorted = sort {$a cmp $b} @totals;
    foreach my $line (@totals_sorted)
    {
    my ($score, $name) = split(/\|/, $line);
    print "$name $score\n";
    }
    I'm not sure what you are trying to do, but couldn't you run the routine twice, once with alpha sorting ascending (blue line above), once with numeric sorting ascending (red line above?

  3. #3
    nishanth's Avatar
    Disabled

    Status
    Offline
    Join Date
    Apr 2006
    Posts
    4

    Re: Sorting with perl

    Hello,

    i am not sure if i am too late.
    Better try hash.
    I am sure your apt datastructure is not array but hash.

    Regards
    Nishanth



Tags for this Thread