Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

by BillKSmith (Prior)
on Dec 01, 2020 at 21:37 UTC ( #11124487=note: print w/replies, xml ) Need Help??


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

Use the function nsort_by in the module List::SomeUtils.
use strict; use warnings; use List::SomeUtils qw(nsort_by); my @list = ( '1,cat', '2,dog', '22,mouse', '11,eel', '001,elk', '13,mi +nk'); my @sorted = nsort_by {/0*(\d+)\,/;$1} @list; {local $" = q(', '); print qq('@sorted'\n);}

OUTPUT:

'1,cat', '001,elk', '2,dog', '11,eel', '13,mink', '22,mouse'
Bill

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2021-04-11 21:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?