This code gives the following output.use strict "vars"; my (@a, @b, $ja); $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[14] = 'D1180'; $b[1] = 'E1180'; $b[2] = 'D0250'; $b[3] = 'A1180'; $b[4] = 'D0130'; $b[5] = 'E0855'; $b[6] = 'E0975'; $b[7] = 'A0130'; $b[8] = 'A0250'; $b[9] = 'B1105'; $b[10] = 'B1225'; $b[11] = 'B2480'; $b[12] = 'C1180'; $b[13] = 'C1600'; $b[14] = 'D1180'; print "\ncmp sort of \@a\n"; foreach $ja (sort{$a cmp $b} @a) { print "$ja\n"; } print "\ncmp sort of \@b\n"; foreach $ja (sort{$a cmp $b} @b) { print "$ja\n"; }
Data in @a is sorted so that the number part is treated a letters. Therefore for the values starting with A, the order of the number part is 1180, 130, 250. What I really want for these values is the order 130, 250, 1180. I have kind of achieved this by adding a zero in front of numbers where there value is less than 1000. This is the data in @b However, the data then becomes A0130, A0250, A1180 which is not ideal. Is there a way of sorting the data in @a so that I get the order shown for @b but without adding the extra and unwanted zeros?cmp sort of @a A1180 A130 A250 B1105 B1225 B2480 C1180 C1600 D1180 D130 D250 E1180 E855 E975 cmp sort of @b A0130 A0250 A1180 B1105 B1225 B2480 C1180 C1600 D0130 D0250 D1180 E0855 E0975 E1180
In reply to Data with Letter(s) & Number sort query by merrymonk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |