There's more than one way to do things PerlMonks

### Re^5: two order sort

by AnomalousMonk (Canon)
 on Mar 05, 2013 at 07:04 UTC ( #1021762=note: print w/replies, xml ) Need Help??

in reply to Re^4: two order sort

```# Decending numeric and descending alpha
...
pack 'Na4a*', -(0+\$_), ~substr( \$_, 1), \$_
...
;;
0uvmu
0dvys
0cjlf
9klou
9ijei
8klfd
...
1imwi
1akkn

I think you fixed a  -substr( \$_, 1) (- vice ~) that was there originally, but I was still puzzled by the sorting of 0 before 9 in an otherwise descending numeric sort. Looking at the bit fields in detail tells the tale: the  -(0+\$_) expression should be  ~(0+\$_) instead (in this particular case at least) to produce an effective descending numeric ordering from an actual ascending lexicographic sort.

```>perl -wMstrict -le
"print unpack 'H*', pack 'N',  -0;
print unpack 'H*', pack 'N',  -1;
print unpack 'H*', pack 'N',  -2;
print unpack 'H*', pack 'N',  -9;
print unpack 'H*', pack 'N', -10;
print '';
;;
print unpack 'H*', pack 'N',  ~0;
print unpack 'H*', pack 'N',  ~1;
print unpack 'H*', pack 'N',  ~2;
print unpack 'H*', pack 'N',  ~9;
print unpack 'H*', pack 'N', ~10;
"
00000000
ffffffff
fffffffe
fffffff7
fffffff6

ffffffff
fffffffe
fffffffd
fffffff6
fffffff5
```>perl -wMstrict -le
"use List::Util qw(shuffle);
;;
my @a = qw(
0uvmu 3rqhx 8klfd 2uwep 9klou 3jrkx 6excm 1imwi 7axqf 1lpys
0dvys 3ozcw 7lbhj 1pjzw 9ijei 3hiyn 3vkon 1akkn 0cjlf 3myrn
);
;;
@a = shuffle @a;
;;
print for map{
unpack 'x[Na4]a*', \$_
} sort map{
local \$^W;  pack 'Na4a*', ~(0+\$_), ~substr( \$_, 1), \$_
} @a
"
9klou
9ijei
8klfd
7lbhj
7axqf
6excm
3vkon
3rqhx
3ozcw
3myrn
3jrkx
3hiyn
2uwep
1pjzw
1lpys
1imwi
1akkn
0uvmu
0dvys
0cjlf

Hmmm...     This is trickier than it looks. And it looks kinda tricky. (And I don't say that my little fix is general.)

Replies are listed 'Best First'.
Re^6: two order sort
by BrowserUk (Pope) on Mar 05, 2013 at 15:11 UTC

You're right of course. You need ~ for both. (Seems I also forgot the details :)

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Create A New User
Node Status?
node history
Node Type: note [id://1021762]
help
Chatterbox?
 [shmem]: same with undef as last element [msh210]: oh, good point. It's the best kind of correct [shmem]: msh210: but I'd not call it a lie outright. That's when you do know the truth. [msh210]: I don't have sendmail (I'm on MS Windows), so does one of you think you can report the bug? [shmem]: msh210: 'tis about time to tell the sad truth to perl5porters - via perlbug [msh210]: probably whoever wrote that actually knows the truth [shmem]: msh210: you can save the perlbug text in a file and send that via your mail agent of choice [msh210]: hm, okay, I'll try, thanks [shmem]: ...not as an attachment, it's probably better to c&p that into the mail body

How do I use this? | Other CB clients
Other Users?
As of 2017-04-24 12:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I'm a fool:

Results (439 votes). Check out past polls.