May 23, 2013

Sort hash by value string in perl


If you want to sort numbers, use <=>
If you want to sort strings, use cmp instead of <=>

Sort hash by value(String) ascending in perl by using the following Syntax:
sort{ $hash{$a} cmp $hash{$b} }

Sort hash by value(String) descending in perl by using the following Syntax:
sort{ $hash{$b} cmp $hash{$a} }

sort_by_hash_value_strings.pl
use strict;
use warnings;
use Data::Dumper;

my %students = (
    Diana    => "Science",
    Davis    => "Science",
    John     => "Maths",
    Linus    => "Physics",
    Brown    => "Maths",
    Jane     => "Science"
);


print "\n\n Before Sorting on Value(Subject): ";
for my $each_student (sort keys %students) {
   print "\n Each Student : $each_student \tSubject : " . $students{$each_student};
}

print "\n\n After Sorting Value(Subject) in Ascending Order : ";
for my $each_student (sort{ $students{$a} cmp $students{$b} } keys %students) {
   print "\n Each Student : $each_student \tSubject : " . $students{$each_student};
}

print "\n\n After Sorting Value(Subject) in Descending Order : ";
for my $each_student (sort{ $students{$b} cmp $students{$a} } keys %students) {
   print "\n Each Student : $each_student \tSubject : " . $students{$each_student};
}
print "\n\n";  


Output:

 Before Sorting on Value(Subject): 
 Each Student : Brown   Subject : Maths
 Each Student : Davis   Subject : Science
 Each Student : Diana   Subject : Science
 Each Student : Jane    Subject : Science
 Each Student : John    Subject : Maths
 Each Student : Linus   Subject : Physics

 After Sorting Value(Subject) in Ascending Order : 
 Each Student : John    Subject : Maths
 Each Student : Brown   Subject : Maths
 Each Student : Linus   Subject : Physics
 Each Student : Jane    Subject : Science
 Each Student : Davis   Subject : Science
 Each Student : Diana   Subject : Science

 After Sorting Value(Subject) in Descending Order : 
 Each Student : Jane    Subject : Science
 Each Student : Davis   Subject : Science
 Each Student : Diana   Subject : Science
 Each Student : Linus   Subject : Physics
 Each Student : John    Subject : Maths
 Each Student : Brown   Subject : Maths



Please refer to other topics in Perl like :
Decimal Sort in Perl
Sort hash by value string in perl
Sort hash by value numerically in perl
How to read command line arguments in Perl
How to pass command line arguments to perl script
Loop through Directory and read the files in Perl
How to create excel report in Perl


Please refer to other topics on Unix like :
Unix Delete Duplicated Lines in a File
Unix Unique Lines in a File
Unix Grep Examples
Unix Cut Command Examples
Search a Directory in Unix
Unix For Loop
pushd & popd in Unix
Find Size of Directory
Word Count


Please refer to other topics on AWK like :
Awk Examples
Print First Two Columns of File
Print Last Two Columns of File


Please refer to other topics on Dict like :
Dict in Python
Dict keys and values in Python


Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension


Please refer to other topics on File Concepts like :
Print File Content in Python
Print File in Reverse Order in Python


Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl


You might also wish to read other topics on Python like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton


No comments:

Post a Comment