Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Sort list by position of items in another list

by alexander_lunev (Pilgrim)
on Feb 18, 2022 at 18:44 UTC ( [id://11141480]=note: print w/replies, xml ) Need Help??


in reply to Sort list by position of items in another list

Oh, I've done exactly this recently, here's the sub for sorting cloth sizes:
my @sizes = qw/6XS 5XS 6XL L S xL m 3xs/; my $sort_sizes_sub = sub { my $uc_a = uc($a); my $uc_b = uc($b); my @sizes_sorted = qw/6XS 5XS 4XS 3XS 2XS XS S M L XL XXL 2XL 3XL 4X +L 5XL 6XL/; return 0 if $uc_a eq $uc_b; foreach my $s (@sizes_sorted) { if ($s eq $uc_a) { return -1; } if ($s eq $uc_b) { return 1; } } }; print join(' ', @sizes). "\n"; print join(' ', sort $sort_sizes_sub @sizes). "\n";

Result:

6XS 5XS 6XL L S xL m 3xs 6XS 5XS 3xs S m L xL 6XL

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2024-04-18 12:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found