in reply to Sorting multi-dimensional arrays

Molt has the problem right, even if his proposed solution is not likely optimal. You will just have to process the elements first so as to make them acceptable numericals. If you always have two alphanumeric characters in front, this may do: @sorted = sort { substr($a->[0], 2) <=> substr($b->[0], 2) } @array; Do be aware of the ubiquitous Schwartzian Transform which can immensely accelerate sorting on expensive to calculate derived keys.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re: Sorting multi-dimensional arrays
by RMGir (Prior) on Sep 11, 2002 at 17:32 UTC
    Unless he's going to sort a VERY large AoAs, cmp should be plenty fast enough. He probably does not need to go to through setting up an ST or GRT.

    And judging from his example, it looks like his keys are fixed width, so he doesn't have to worry about "CG100" vs "CG1000", for instance.
    --
    Mike

      Even fixed length strings are not really a guarantee; cmp will do the wrong thing for CC100A vs CC1000 too. It's always better to be safe than sorry. Note my code sample did not use a Schwartzian Transform because if the data set is as small as what he showed, it isn't worth the CPU time to set it up; I merely wanted him to be aware of it, should he need such.

      Makeshifts last the longest.

        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