in reply to Re^3: Sorting multi-dimensional arrays
in thread Sorting multi-dimensional arrays

Good point about making him aware of the ST option.

However, why are you so sure that lexicographic sorting would be wrong? It's quite possible that CC100A _does_ go after CC1000, in his system... (Well, ok, CCI10A vs CCI100, to follow his format more closely).

The sort order is under-specified, but it appears from his one example that cmp would be satisfactory... I admit that a 4 item example is a bit slim :)
--
Mike

Replies are listed 'Best First'.
Re^5: Sorting multi-dimensional arrays
by Aristotle (Chancellor) on Sep 11, 2002 at 18:39 UTC
    It's quite possible that CC100A _does_ go after CC1000, in his system...
    That would be what I call an accidental feature.. :-) Better to be safe than sorry, remember? The substr solution will work regardless of circumstances and won't surprisingly blow up if he moves the script to a system with different alphanumeric order. I am in no way dismissing cmp off hand as it is indeed a candidate if you are absolutely certain about your data format. But that's just what I find to be a dangerous assumption. I mean, who needs four digit years, right? :-) See also my signature..

    Makeshifts last the longest.

      The substr solution will work regardless of circumstances. . .

      What about with CCI001 and CDI001? I think RMGir is right: the sort order is underspecified. Even something like:

      # Simplified for illustration. Will not work for OP. Would benefit fro +m ST. sort { my ($a1,$a2) = $a =~ /([A-Z]*)(\d*)/; my ($b1,$b2) = $b =~ /([A-Z]*)(\d*)/; $a1 cmp $b1 || $a2 <=> $b2 } @list;
      is likely to break with CC100A and CC100B. Given the elements that he gave us I'd think that a variation on the above would work but that guess isn't any better than yours or anyone's.

      -sauoq
      "My two cents aren't worth a dime.";
      
        Note that yours is making even fewer assumptions than mine, which makes fewer assumptions than cmp. Mine might be making too many assumptions - we can't tell. Your solution is better. The cmp on the other hand is very likely to be making too many assumptions. And that's what I was arguing - to make fewer assumptions.

        Makeshifts last the longest.