in reply to Data with Letter(s) & Number sort query

What you want is a natural sort then. The usual approach is to create for each element to be sorted a string that sorts lexically but from which you can find the original string again. For example, you could build your string by having the padded string, a \0 and then the original string:

my @elements = map { join "\0", padded($_), $_ } @original; @elements = sort { $a cmp $b } @elements; my @sorted = map { ( split /\0/, $_ )[1] } @elements;

You can even chain it all together like the following:

my @elements = map { ( split /\0/, $_ )[1] } sort { $a cmp $b } map { join "\0", padded($_), $_ } @original;

Replies are listed 'Best First'.
Re^2: Data with Letter(s) & Number sort query
by AnomalousMonk (Archbishop) on Nov 19, 2016 at 18:37 UTC
    sort { $a cmp $b }

    Note that the  $a cmp $b "lexicographic ascending" sorting order is the default ordering of sort, so this expression can be replaced with just  sort (i.e, no  { $a cmp $b } code block) wherever it appears here for a significant speed increase for sufficiently large data arrays — but they may have to be quite large!


    Give a man a fish:  <%-{-{-{-<

Re^2: Data with Letter(s) & Number sort query
by hippo (Archbishop) on Nov 19, 2016 at 10:08 UTC

    Are you importing padded() from a module? I don't immediately see where that comes from.

      No, padded is supposed to be implemented by merrymonk, as I understood it that they had the padding already but wanted to undo it after sorting.

        Sorry I gave the wrong impression. I added the 0 before numbers less than 1000 manually. I guess I could create such a function but it would be 'nice' to avoid that.
      Yes, same question to Corion, which is why I just added another complete solution below.