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

Re: Sort Empty String Last

by tadman (Prior)
on Jul 29, 2003 at 17:42 UTC ( [id://278903]=note: print w/replies, xml ) Need Help??


in reply to sort empty string

Using a custom sort routine, you can redefine the sort behavior for a specific condition. Here's an example of something that always puts empty strings last, but otherwise sorts alphabetically:
sub by_blanklast { if (!length($a->[0]) ^ !length($b->[0])) { return length($b->[0]) - length($a->[0]); } return $a->[0] cmp $b->[0]; }
You can use this in your code like this:
 } sort by_blanklast map {
In my testing, this seems to work like you want.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-03-28 10:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found