Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: How can I sort my array numerically on part of the string?

by alexander_lunev (Pilgrim)
on Dec 01, 2020 at 19:57 UTC ( [id://11124471]=note: print w/replies, xml ) Need Help??


in reply to How can I sort my array numerically on part of the string?

While you can sort it automagically (as jdporter already says) with just sort { $a <=> $b } @list, you also can do it in two lines (but still they're simpler than those regexps). First of all, prepare your data so you can use simpler tools to process it. For each list member you have two fields of data in one string, and for me it's a hash that looked at me from that @list.

my @list = ( '1,cat', '2,dog', '22,mouse', '11,eel', '001,elk', '13,mi +nk'); my %unsorted = map { split /,/ } @list ; my @sorted = map { $_.','.$unsorted{$_} } sort { $a <=> $b } keys %uns +orted;

But maybe we're not so lucky and our data will not be prepended with right numerical indexes. What to do? Just reverse the hash pair!

my @list = ( 'cat,1', 'dog,2', 'mouse,22', 'eel,11', 'elk,001', 'mink, +13'); my %unsorted = map { reverse split /,/ } @list ; my @sorted = map { $unsorted{$_}.','.$_ } sort { $a <=> $b } keys %uns +orted;

Now, what if we're totally unlucky and our numerical index not only sit in the end of string, but sometimes equals to another index? We will lose some data in a hash, if we just split index from string and make pairs from them! Could the code still be two lines and still solve the problem and remain readable and understandable?

my @list = ( 'cat,1', 'dog,1', 'mouse,22', 'eel,22', 'elk,001', 'mink, +13'); # as pointed by choroba, this can be put simpler # my %unsorted = map { $_ => [ reverse split /,/ ]->[0] } @list ; my %unsorted = map { $_ => [ split /,/ ]->[-1] } @list ; my @sorted = sort { $unsorted{$a} <=> $unsorted{$b} } keys %unsorted;

TIMTOWTDI!

p.s.: Updated last code as choroba pointed.

Replies are listed 'Best First'.
Re^2: How can I sort my array numerically on part of the string?
by choroba (Cardinal) on Dec 01, 2020 at 20:21 UTC
    Note that
    [ reverse split /,/ ]->[0]

    can be replaced by shorter

    (split /,/)[-1]

    which is also faster.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Of course! Thank you!
Re^2: How can I sort my array numerically on part of the string?
by misterperl (Pilgrim) on Dec 01, 2020 at 21:02 UTC
    TY very helpful I'll try these.

    I'd hoped for an in-situ variety of $a spaceship $b ... But maybe no such luck!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-23 18:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found