The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #5 (permalink)  
Old 10-09-2008
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
Yes, you can sort with a custom rule. But what exactly is the rule for the sort? If you cannot work out a formal specification of the sort, no one will be able to do it "correctly".

The following gives the same ordering as you mentioned, but there is no guarantee that it is going to give you the "correct" ordering (as you might expect) for other input values:


Code:
use Data::Dumper;

@A = qw(cd1 a1 ef a2 hij a12 b2 b4 b22);
print Dumper([sort {
	my ($_a, $_b);
	for ([$a, \$_a], [$b, \$_b]) {
		$_->[0] =~ /^(.+?)(\d*)$/;
		${$_->[1]} = [$1, $2];
	}
	($$_a[0] cmp $$_b[0]) || ($$_a[1] <=> $$_b[1]);
} @A]);