Thanks for your script. I tried it out, and its not exactly right, so the data in my file appears not to conform to the numeric ordering.
Your numeric sort gets all the examples I gave right except Set 5. The original ordering in the file is:
0908 09088122595 09088122595 0909-114
whereas the output of your script on set 5 is:
0908 0909-114 09088122595 09088122595
which is incidentally the answer your script gives for the *ASCII* comparison. So, this is kind of wierd. Do you have an idea what is going on?
I did write a comparison function that seems to work on all examples in my file, but I'm reluctant to post it because its naive, fairly long, and its very slow, which is the reason for my initial post. I tested it by running through each line in my file and making sure my comparison function returned -1 when that was compared to the next line in the file.
Thanks again for your help. Any other hints?
Dan
Shawn McKinley wrote:
Hello Dan, To come up with the sort order you have here, you are sorting on a numeric comparison (not ASCII). If you want to sort in the 'normal' fashion, just use the default ASCII sort. Here is a simple demo:
use strict; my $sets= { set1=>['0-CELEBRITY-0','0-CELEBRITY'], set2=>['0-0-7','0-0'], set3=>['000-AAA','000'], set4=>['000','0000'], set5=>['0908','09088122595','09088122595','0909-114'] }; print 'Sorting on numeric comparison',$/,$/; for(sort keys %$sets) { print ' ',$_,$/; # Numeric sort below for(sort { $a <=> $b } @{$sets->{$_}}) { print ' ',$_,$/; } print $/; } print $/,'Sorting on ASCII comparison',$/,$/; for(sort keys %$sets) { print ' ',$_,$/; # ASCII sort (same as sort { $a cmp $b }) for(sort @{$sets->{$_}}) { print ' ',$_,$/; } print $/; } exit;
Shawn
-----Original Message-----
From: Dan LaFlamme [mailto:[EMAIL PROTECTED] Sent: Tuesday, January 13, 2004 6:13 PM
To: [EMAIL PROTECTED]
Subject: strange sort order
Hi,
I have a file that appears to be somewhat sorted, but is not sorted according to the traditional unix sort. I'll give some examples, and if anyone recgonizes the way in which the file is sorted, please let me know. Also, since I may have to write a comparator function for use on this "sorted" file, any tips on doing that in the most efficient way possible would be helpful. Thanks.
Examples. I give some sets of strings to indicate which comes before the
other in the "sorted" file. The first one in each pair is less than the second. Each set is independent of the others. set x doesnt necessarily come before set y in the file if x < y.
Set 1: 0-CELEBRITY-0 0-CELEBRITY
Set 2: 0-0-7 0-0
Set 3: 000-AAA 000
Set 4: 000 0000
Set 5: 0908 09088122595 09088122595 0909-114
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>