Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

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

by tybalt89 (Monsignor)
on Dec 01, 2020 at 21:15 UTC ( [id://11124483]=note: print w/replies, xml ) Need Help??


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

You were sooo close...

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11124461 use warnings; my @list = ( '1,cat', '2,dog', '22,mouse', '11,eel', '001,elk', '13,mi +nk'); print join "\n", sort { $a=~s/,.+//r <=> $b =~ s/,.+//r } @list;

Outputs:

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-19 11:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found