Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: sorting question

by kabel (Chaplain)
on Nov 21, 2002 at 06:14 UTC ( #214714=note: print w/ replies, xml ) Need Help??

in reply to sorting question

for short: the split accepts a regexp as first parameter, wherein the | is a metasymbol, and you did NOT quote it properly ... ;)

use strict; use warnings; use constant FIRST_FIELD => 1; use constant SECOND_FIELD => 2; use constant THIRD_FIELD => 3; my $str = ""; { local $/; $str = <DATA>; } $str = join "\n", map { $_->[0] } sort {$a->[THIRD_FIELD] cmp $b->[THIRD_FIELD] } map { [$_, split('\|', $_)] } split(/\n/, $str); print $str; __DATA__ xyz4|1026|CR xyz3|3461|CR dert5|3251|PR rtyaq|251|PR dbca|583|PR xxxt|360|CR
note further that cmp does NOT work for numerics, or look for yourself what is going wrong :) you need the <=> operator to do numeric sorting in this way.
and you saw the characters "ST" or "schwartzian transform" along with the code? ;)

UPDATE: added ST line

Comment on Re: sorting question
Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2016-02-12 13:13 GMT
Find Nodes?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

    Results (397 votes), past polls