#!/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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |