Thursday, January 6, 2011

Advanced sorting of normal arrays, hash/ associative arrays in Perl


The following example shows all possible types of sorting in a normal array and also 
in a hash/ associative array through Perl. 

All The possible types of sorting are as follows :

1) Normal arrays = 2 types (ASCII wise sorting, numerical wise/ alphabetical wise 
sorting) X 2 ways (ascending/ normal order, descending order)

Total we have 4 alternatives for sorting a normal array. 

2) Hash/ Associative arrays:

 i) Sorting on the basis of keys = 2 types (ASCII wise sorting, numerical wise/ alphabetical wise sorting) X 2 ways (ascending/ normal order, descending order)
 
 Total we have 4 alternatives for sorting a hash/ associative arrays based on keys.

 ii) Sorting on the basis of values = 1 type (numerical wise/ alphabetical wise sorting) X 2 ways (ascending/ normal order, descending order)
 
 Total we have 2 alternatives for sorting a hash/ associative arrays based on values.


#!/usr/bin/perl

print "content-type: text/html \n\n";

###### sorting of an array with numeric values ########

@nums=(1,5,4,6,3,7,2,8,10,9);

print "Unsorted numbers from array: @nums\n";

@sorted_nums=sort(@nums); # ASCII wise sorting of normal array and storing the sorted result in another array

print "ASCII wise sorted numbers from array: @sorted_nums\n";

@sorted_nums=sort { $a <=> $b } @nums; # Numeric wise sorting of normal array and storing the sorted result in another array

print "Numeric wise sorted numbers from array: @sorted_nums\n";

@sorted_nums=sort { $b <=> $a } @nums; # Numeric wise sorting of normal array in reverse order and storing the sorted result in another array

print "Numeric wise sorted numbers in reverse order from array: @sorted_nums\n";


###### sorting of an array with character values ########

@chars=("B","a","d","C","E","f");

print "Unsorted characters from array: @chars\n";

@sorted_chars=sort(@chars); # ASCII wise sorting of normal array and storing the sorted result in another array

print "ASCII wise sorted characters from array: @sorted_chars\n";

@sorted_chars=sort { lc($a) cmp lc($b) } @chars; # Alphabet wise sorting of normal array and storing the sorted result in another array

print "Alphabetical wise sorted characters from array: @sorted_chars\n";

@sorted_chars=sort { lc($b) cmp lc($a) } @chars; # Alphabet wise sorting of normal array in reverse order and storing the sorted result in another array

print "Alphabetical wise sorted characters in reverse order from array: @sorted_chars\n";


###### sorting of hash/ associative arrays based on keys ########

%num_hash=(
 1 => "one",
 2 => "two",
 3 => "three",
 4 => "four",
 5 => "five",
 6 => "six",
 7 => "seven",
 8 => "eight",
 9 => "nine",
 10 => "ten",
);

print "\n-------------------------------------------\n";

foreach $key (sort keys%num_hash) # ASCII wise sorting of keys in hash/ associative array and displaying the sorted result on screen
{
 print "ASCII wise sorted numeric keys from hash: key=$key value=$num_hash{$key}\n";
}

print "\n-------------------------------------------\n";

foreach $key (reverse sort keys%num_hash) # ASCII wise sorting of keys in hash/ associative array in reverse order and displaying the sorted result on screen
{
 print "ASCII wise sorted numeric keys in reverse order from hash: key=$key value=$num_hash{$key}\n";
}

print "\n-------------------------------------------\n";

foreach $key (sort { $a <=> $b } keys%num_hash) # Numeric wise sorting of keys in hash/ associative array and displaying the sorted result on screen
{
 print "Numeric wise sorted numeric keys from hash: key=$key value=$num_hash{$key}\n";
}

print "\n-------------------------------------------\n";

foreach $key (sort { $b <=> $a } keys%num_hash) # Numeric wise sorting of keys in hash/ associative array in reverse order and displaying the sorted result on screen
{
 print "Numeric wise sorted numeric keys in reverse order from hash: key=$key value=$num_hash{$key}\n";
}


%char_hash=(
 "B" => "ball",
 "a" => "apple",
 "d" => "doll",
 "C" => "cat",
 "E" => "elephant",
 "f" => "fox",
);

print "\n-------------------------------------------\n";

foreach $key (sort keys%char_hash) # ASCII wise sorting of keys in hash/ associative array and displaying the sorted result on screen
{
 print "ASCII wise sorted character keys from hash: key=$key value=$char_hash{$key}\n";
}

print "\n-------------------------------------------\n";

foreach $key (reverse sort keys%char_hash) # ASCII wise sorting of keys in hash/ associative array in reverse order and displaying the sorted result on screen
{
 print "ASCII wise sorted character keys in reverse order from hash: key=$key value=$char_hash{$key}\n";
}

print "\n-------------------------------------------\n";

foreach $key (sort { lc($a) cmp lc($b) } keys%char_hash) # Alphabet wise sorting of keys in hash/ associative array and displaying the sorted result on screen
{
 print "Alplabetical wise sorted character keys from hash: key=$key value=$char_hash{$key}\n";
}

print "\n-------------------------------------------\n";

foreach $key (sort { lc($b) cmp lc($a) } keys%char_hash) # Alphabet wise sorting of keys in hash/ associative array in reverse order and displaying the sorted result on screen
{
 print "Alplabetical wise sorted character keys in reverse order from hash: key=$key value=$char_hash{$key}\n";
}


###### sorting of hash/ associative arrays based on values ########

%num_hash=(
 "one",1,
 "two",2,
 "three",3,
 "four",4,
 "five",5,
 "six",6,
 "seven",7,
 "eight",8,
 "nine",9,
 "ten",10,
);

print "\n-------------------------------------------\n";

foreach $key (sort { $num_hash{$a} <=> $num_hash{$b} } keys%num_hash) # Numeric wise sorting of values in hash/ associative array and displaying the sorted result on screen
{
 print "Numeric wise sorted numeric values from hash: key=$key value=$num_hash{$key}\n";
}

print "\n-------------------------------------------\n";

foreach $key (sort { $num_hash{$b} <=> $num_hash{$a} } keys%num_hash) # Numeric wise sorting of values in hash/ associative array in reverse order and displaying the sorted result on screen
{
 print "Numeric wise sorted numeric values in reverse order from hash: key=$key value=$num_hash{$key}\n";
}


%char_hash=(
 "a" => "Ball",
 "b" => "apple",
 "c" => "Cat",
 "d" => "doll",
 "e" => "Elephant",
 "f" => "fox",
);

print "\n-------------------------------------------\n";

foreach $key (sort { lc($char_hash{$a}) cmp lc($char_hash{$b}) } keys%char_hash) # Alphabet wise sorting of values in hash/ associative array and displaying the sorted result on screen
{
 print "Alplabetical wise sorted character values from hash: key=$key value=$char_hash{$key}\n";
}

print "\n-------------------------------------------\n";

foreach $key (sort { lc($char_hash{$b}) cmp lc($char_hash{$a}) } keys%char_hash) # Alphabet wise sorting of values in hash/ associative array in reverse order and displaying the sorted result on screen
{
 print "Alplabetical wise sorted character values in reverse order from hash: key=$key value=$char_hash{$key}\n";
}

Output:
-------
Unsorted numbers from array: 1 5 4 6 3 7 2 8 10 9
ASCII wise sorted numbers from array: 1 10 2 3 4 5 6 7 8 9
Numeric wise sorted numbers from array: 1 2 3 4 5 6 7 8 9 10
Numeric wise sorted numbers in reverse order from array: 10 9 8 7 6 5 4 3 2 1
Unsorted characters from array: B a d C E f
ASCII wise sorted characters from array: B C E a d f
Alphabetical wise sorted characters from array: a B C d E f
Alphabetical wise sorted characters in reverse order from array: f E d C B a

-------------------------------------------
ASCII wise sorted numeric keys from hash: key=1 value=one
ASCII wise sorted numeric keys from hash: key=10 value=ten
ASCII wise sorted numeric keys from hash: key=2 value=two
ASCII wise sorted numeric keys from hash: key=3 value=three
ASCII wise sorted numeric keys from hash: key=4 value=four
ASCII wise sorted numeric keys from hash: key=5 value=five
ASCII wise sorted numeric keys from hash: key=6 value=six
ASCII wise sorted numeric keys from hash: key=7 value=seven
ASCII wise sorted numeric keys from hash: key=8 value=eight
ASCII wise sorted numeric keys from hash: key=9 value=nine

-------------------------------------------
ASCII wise sorted numeric keys in reverse order from hash: key=9 value=nine
ASCII wise sorted numeric keys in reverse order from hash: key=8 value=eight
ASCII wise sorted numeric keys in reverse order from hash: key=7 value=seven
ASCII wise sorted numeric keys in reverse order from hash: key=6 value=six
ASCII wise sorted numeric keys in reverse order from hash: key=5 value=five
ASCII wise sorted numeric keys in reverse order from hash: key=4 value=four
ASCII wise sorted numeric keys in reverse order from hash: key=3 value=three
ASCII wise sorted numeric keys in reverse order from hash: key=2 value=two
ASCII wise sorted numeric keys in reverse order from hash: key=10 value=ten
ASCII wise sorted numeric keys in reverse order from hash: key=1 value=one

-------------------------------------------
Numeric wise sorted numeric keys from hash: key=1 value=one
Numeric wise sorted numeric keys from hash: key=2 value=two
Numeric wise sorted numeric keys from hash: key=3 value=three
Numeric wise sorted numeric keys from hash: key=4 value=four
Numeric wise sorted numeric keys from hash: key=5 value=five
Numeric wise sorted numeric keys from hash: key=6 value=six
Numeric wise sorted numeric keys from hash: key=7 value=seven
Numeric wise sorted numeric keys from hash: key=8 value=eight
Numeric wise sorted numeric keys from hash: key=9 value=nine
Numeric wise sorted numeric keys from hash: key=10 value=ten

-------------------------------------------
Numeric wise sorted numeric keys in reverse order from hash: key=10 value=ten
Numeric wise sorted numeric keys in reverse order from hash: key=9 value=nine
Numeric wise sorted numeric keys in reverse order from hash: key=8 value=eight
Numeric wise sorted numeric keys in reverse order from hash: key=7 value=seven
Numeric wise sorted numeric keys in reverse order from hash: key=6 value=six
Numeric wise sorted numeric keys in reverse order from hash: key=5 value=five
Numeric wise sorted numeric keys in reverse order from hash: key=4 value=four
Numeric wise sorted numeric keys in reverse order from hash: key=3 value=three
Numeric wise sorted numeric keys in reverse order from hash: key=2 value=two
Numeric wise sorted numeric keys in reverse order from hash: key=1 value=one

-------------------------------------------
ASCII wise sorted character keys from hash: key=B value=ball
ASCII wise sorted character keys from hash: key=C value=cat
ASCII wise sorted character keys from hash: key=E value=elephant
ASCII wise sorted character keys from hash: key=a value=apple
ASCII wise sorted character keys from hash: key=d value=doll
ASCII wise sorted character keys from hash: key=f value=fox

-------------------------------------------
ASCII wise sorted character keys in reverse order from hash: key=f value=fox
ASCII wise sorted character keys in reverse order from hash: key=d value=doll
ASCII wise sorted character keys in reverse order from hash: key=a value=apple
ASCII wise sorted character keys in reverse order from hash: key=E value=elephant
ASCII wise sorted character keys in reverse order from hash: key=C value=cat
ASCII wise sorted character keys in reverse order from hash: key=B value=ball

-------------------------------------------
Alplabetical wise sorted character keys from hash: key=a value=apple
Alplabetical wise sorted character keys from hash: key=B value=ball
Alplabetical wise sorted character keys from hash: key=C value=cat
Alplabetical wise sorted character keys from hash: key=d value=doll
Alplabetical wise sorted character keys from hash: key=E value=elephant
Alplabetical wise sorted character keys from hash: key=f value=fox

-------------------------------------------
Alplabetical wise sorted character keys in reverse order from hash: key=f value=fox
Alplabetical wise sorted character keys in reverse order from hash: key=E value=elephant
Alplabetical wise sorted character keys in reverse order from hash: key=d value=doll
Alplabetical wise sorted character keys in reverse order from hash: key=C value=cat
Alplabetical wise sorted character keys in reverse order from hash: key=B value=ball
Alplabetical wise sorted character keys in reverse order from hash: key=a value=apple

-------------------------------------------
Numeric wise sorted numeric values from hash: key=one value=1
Numeric wise sorted numeric values from hash: key=two value=2
Numeric wise sorted numeric values from hash: key=three value=3
Numeric wise sorted numeric values from hash: key=four value=4
Numeric wise sorted numeric values from hash: key=five value=5
Numeric wise sorted numeric values from hash: key=six value=6
Numeric wise sorted numeric values from hash: key=seven value=7
Numeric wise sorted numeric values from hash: key=eight value=8
Numeric wise sorted numeric values from hash: key=nine value=9
Numeric wise sorted numeric values from hash: key=ten value=10

-------------------------------------------
Numeric wise sorted numeric values in reverse order from hash: key=ten value=10
Numeric wise sorted numeric values in reverse order from hash: key=nine value=9
Numeric wise sorted numeric values in reverse order from hash: key=eight value=8
Numeric wise sorted numeric values in reverse order from hash: key=seven value=7
Numeric wise sorted numeric values in reverse order from hash: key=six value=6
Numeric wise sorted numeric values in reverse order from hash: key=five value=5
Numeric wise sorted numeric values in reverse order from hash: key=four value=4
Numeric wise sorted numeric values in reverse order from hash: key=three value=3
Numeric wise sorted numeric values in reverse order from hash: key=two value=2
Numeric wise sorted numeric values in reverse order from hash: key=one value=1

-------------------------------------------
Alplabetical wise sorted character values from hash: key=b value=apple
Alplabetical wise sorted character values from hash: key=a value=Ball
Alplabetical wise sorted character values from hash: key=c value=Cat
Alplabetical wise sorted character values from hash: key=d value=doll
Alplabetical wise sorted character values from hash: key=e value=Elephant
Alplabetical wise sorted character values from hash: key=f value=fox

-------------------------------------------
Alplabetical wise sorted character values in reverse order from hash: key=f value=fox
Alplabetical wise sorted character values in reverse order from hash: key=e value=Elephant
Alplabetical wise sorted character values in reverse order from hash: key=d value=doll
Alplabetical wise sorted character values in reverse order from hash: key=c value=Cat
Alplabetical wise sorted character values in reverse order from hash: key=a value=Ball
Alplabetical wise sorted character values in reverse order from hash: key=b value=apple