But this wont work if my input data can be like this...
A1234a
c2345a
B1234a
a1235a
c1235c
b1235b
i have got a code in perl... but cud not make out wat the hash sort means.. can any1 help?
#!/bin/perl
my @order=("a", "b", "c", "1", "2", "3", "A", "B", "C","z"); # Order Characters; Entire EBCDIC character set needs to be defined here
my %sorthash; # For quicker sorting the sub initiliaze() puts the list @order into a hash.
my @strings=("ABC123", "123ABC", "abcABC", "ABCabc", "abc123", "123abc","zzz123"); # Sort List
initialize(); # Puts the ordering into a hash of the format ("a" => 1, "A" => 2, "\342" => 3, "\302" => 4, ...)
my $string;
foreach $string (sort @strings) { # Normal way of sorting in perl, but sort now calls "mysort" for getting the right ordering
print $string."\n";
}
sub mysort { # Compares two elements x and y
my $word1=$a;
my $word2=$b;
return 0 if ($word1 eq $word2);
my @word1=split("", $word1);
my @word2=split("", $word2);
while ((@word1 > 0) and (@word2 > 0)) {
my $char1=shift @word1;
my $char2=shift @word2;
my $compare=($sorthash{$char2}<=>$sorthash{$char1});
return $compare if ($compare != 0);
}
if (@word1) {
return 1;
} else {
return -1;
}
}
sub initialize {
my $i=1;
my $entry;
foreach $entry (@order) {
$sorthash{$entry}=$i;
$i++;
}
}