Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

by tybalt89 (Prior)
on Dec 01, 2020 at 21:15 UTC ( #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
Node Status?
node history
Node Type: note [id://11124483]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2021-03-04 22:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favorite kind of desktop background is:











    Results (108 votes). Check out past polls.

    Notices?