Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: How to sort array by columns maintaining the rows? (for C style arrays)

by Anonymous Monk
on Jul 02, 2000 at 19:41 UTC ( [id://20803]=note: print w/replies, xml ) Need Help??


in reply to How to sort array by columns maintaining the rows? (for C style arrays)

the following code shows how to sort this data structure by any of its three columns.
@data = ( [qw(marek brutalski 20)], [qw(zenia markownikowa 10)], [qw(teresa parufkowa 90)], [qw(bogumila pierdawa 40)], [qw(genowefa tempawa 50)], ); # Sort by first name @sorted_by_first = sort { $a->[0] cmp $b->[0] } @data; # Sort by last name @sorted_by_last = sort { $a->[1] cmp $b->[1] } @data; # Sort by score @sorted_by_score = sort { $a->[2] <=> $b->[2] } @data;
notice that the sorts on columns 0 and 1, the names, are performed ASCIIbetically, while the sort by number is done numerically.
  • Comment on Re: How to sort array by columns maintaining the rows? (for C style arrays)
  • Download Code

Replies are listed 'Best First'.
RE: RE: How to sort array by columns maintaining the rows? (for C style arrays)
by Anonymous Monk on Jul 03, 2000 at 18:20 UTC
    First, "progressive sorting" does not work as expected: Each sort step destroys the order created by the previous sorts. You need to use the || operator as described in the above answers.

    Second, to sort your line speeds, create a hash that converts the names to sortable numbers:

    my $n=1; for (qw(115kbps 2Mbps 10Mbps T1)) {$order{$_}=$n++}
    Now, you can sort line speeds. Suppose that your list @connections contains records with the line speed in the first element (index 0). Thus,
    @result= sort {$order{$a->[0]} <=> $order{$b->[0]}} @connections;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://20803]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (None)
    As of 2024-04-25 03:58 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found