sort hash in perl

%aList = ('car'=>300,'train'=>150,'truck'=>100,'bike'=>330);

I need to write a program to sort this by the order of key and value respectively without
using the sort functions provided by the language
ytt5509ExpertsAsked:
Who is Participating?
 
jmcgOwnerCommented:
If you were interviewing with me, you'd be in trouble until you explained that you didn't really mean "sort the hash". The elements of the hash can be retrieved in a specific order, most commonly by sorting the keys. And there are modules that support expanded hash objects with additional properties, such as maintaining the insertion order.
0
 
Perl_DiverCommented:
Is this some type of school assignment? Why can't you use perls sort() function?
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Perl_DiverCommented:
Can we earn points just from posting links to articles? Seems like anyone could post links.
0
 
ytt5509ExpertsAuthor Commented:
I'm preparing a sets of questions and answers for my interview, not school work.
And, I really need a workable version of functions, not links, Thanks.
0
 
Perl_DiverCommented:
then maybe you want to start looking at the relational and equality operators, stuff like:

>
<
>=
<=
gt
lt
ge
le
eq
ne
cmp
<=>
0
 
mjcoyneCommented:
Why can you not use sort?  Are you saying that you can't do anything like:

#!/usr/bin/perl -w
use strict;

    my %aList = ('car'=>300,'train'=>150,'truck'=>100,'bike'=>330);
   
    print "Sorted by key, alphabetically:\n\n";
    foreach my $key (sort keys(%aList)) {
       print "\t$key = $aList{$key}\n";
    }
   
    print "\nSorted by key, reverse alphabetically:\n\n";
    foreach my $key (reverse (sort keys(%aList))) {
       print "\t$key = $aList{$key}\n";
    }
   
    print "\nSorted by value, lowest to highest:\n\n";
    foreach my $key (sort value_L2H (keys(%aList))) {
       print "\t$key = $aList{$key}\n";
    }
   
    print "\nSorted by value, highest to lowest:\n\n";
    foreach my $key (sort value_H2L (keys(%aList))) {
       print "\t$key = $aList{$key}\n";
    }

sub value_L2H {
   $aList{$a} <=> $aList{$b};
}

sub value_H2L {
   $aList{$b} <=> $aList{$a};
}

0
 
ytt5509ExpertsAuthor Commented:
this is the question that made up by the supervisor, and our job is to turn in
the answer, and myself is not a decent perl programmer. What I mean for
the problem to solve is:

is it possible to print out a hash contents in ASCII order with respect to the
key(car, bike, truck) and the value(330, 300, 150), without using the "sort" function?
The result will be the same like mjcoyne has provided, but without using "sort".
0
 
ozoCommented:
You can
use Tie::IxHash;
to keep hashes sorted
0
 
jmcgOwnerCommented:
So is it a question of using some clever thing, like the fancier hash Ozo has pointed to (with the SortByKey and SortByValue re-ordering methods -- what do you want to bet that they're implemented using 'sort'?) or of implementing your own sort function in Perl?
0
 
mjcoyneCommented:
My answer to such a question would be:  "Yes, a function can be written in Perl (or as a C extension) that will sort a hash any way you like without relying on Perl's internal sort function.  However, since I'll be working for you and you'll be paying me by the hour, I would never waste time on such a pursuit, opting instead to rely on Perl's well-known and clearly predictable internal function.  These types of decisions would allow me to work more efficiently by remaining focused on the purpose of the code I'm writing, rather than meandering into code backwaters which might actually introduce subtle and hard-to-track bugs into the production code".

:)

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.