#!/usr/bin/perl # http://perlmonks.org/?node_id=1176132 use strict; use warnings; my @a; $a[1] = 'E1180'; $a[2] = 'D250'; $a[3] = 'A1180'; $a[4] = 'D130'; $a[5] = 'E855'; $a[6] = 'E975'; $a[7] = 'A130'; $a[8] = 'A250'; $a[9] = 'B1105'; $a[10] = 'B1225'; $a[11] = 'B2480'; $a[12] = 'C1180'; $a[13] = 'C1600'; $a[0] = 'D1180'; print "\nsort of \@a\n"; for my $ja (sort{$a =~ tr/0-9//cdr <=> $b =~ tr/0-9//cdr} sort @a) { print "$ja\n"; }

produces

sort of @a A130 D130 A250 D250 E855 E975 B1105 A1180 C1180 D1180 E1180 B1225 C1600 B2480

I haven't seen this version here yet. It sorts first by alpha, then by comparisons after stripping off all non-numbers. Why two sorts? Why not? How does the second (numeric) sort keep from mixing up the first sort? Because recent perl sorts are stable.

So is it faster or slower than the other sorts given here? Who knows? Who cares? If you really care, then benchmark...


In reply to Re: Data with Letter(s) & Number sort query by tybalt89
in thread Data with Letter(s) & Number sort query by merrymonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.