in reply to Re: "numeric" sort of keys?
in thread "numeric" sort of keys?

This is exactly what I asked for BUT, I left out some detail on the structure of the keys. The text "field" of the key can vary in length and it is not always the word "Drive". My current list has Drive, IRMA, CMS, PRD and some others as the first "field" of the key and one or two digits as the second "field" of the key. The key is a combination of text and numbers and I want to sort first on the text "field" (which is not unique) and then sort on the numeric "field" of the key. Your help has pointed me in the right direction though. I see how I can iterate through each character until I find the first numeric character and calculate which column to split on. Or can I do this with some sort of regex and the split command? I'll go work it out and post my solution. Thanks to all for the help!!!!

Gorio

Replies are listed 'Best First'.
Re: Re: Re: "numeric" sort of keys?
by Limbic~Region (Chancellor) on Sep 25, 2003 at 21:24 UTC
    Gorio3721,
    My first attempt at the Schwartzian Transform.
    #!/usr/bin/perl -w use strict; my @keys = qw (CMS5 Drive1 Drive2 Drive10 IRMA1 IRMA13 IRMA2); my %hash; @hash{@keys} = (); for my $key ( custom_sort(\%hash) ) { print "Key: $key\n"; } sub custom_sort { my $hash = shift; return map { $_->[0] } sort { $a->[1] cmp $b->[1] || $a->[2] <=> $b->[2]} map { [ $_, /(\w+?)(\d+)$/ ] } keys %$hash; } __END__ Key: CMS5 Key: Drive1 Key: Drive2 Key: Drive10 Key: IRMA1 Key: IRMA2 Key: IRMA13

    Cheers - L~R