sub return_sorted
{
# Declare all our variables as local to this subroutine:
my ($hashref,$sortby,$order,%data,@keys);

$hashref = shift;		# Get a reference to the hash we wish to sort.
%data = %{$hashref};	# Convert the above reference into a real hash.
$sortby = shift;		# Get the keyname to sort by.
$order = shift;			# Sort ascendingly or descendingly ?


# Here we do the actual sorting of the hash, using the keyname provided above.
# We'll always be dealing with 2 dimensional hash's, so we use the following:

# 1. If we're sorting by entry, date or time (all treated alike), we'll sort numerically.
# 2. If we're sorting ny anything else, we'll sort alphabetically.

# N.B. We sort descendingly by default, if necessary we can reverse it later.

if ($sortby =~ /(entry|date|time)/){
 @keys = sort { $data{$b}{'entry'} <=> $data{$a}{'entry'} } keys %data;
}else{
 @keys = sort { $data{$b}{$sortby} cmp $data{$a}{$sortby} } keys %data;
}


# If we're asked to sort ascendingly, we just reverse the array:
if ($order eq "asc"){ @keys = reverse(@keys); }

# Now we just return the sorted list of keys:
return @keys;

# N.B.
# There is no way to re-sort the hash itself, so we have to use an array of keynames.
# More information on this is available via perldoc.
}

1;