#!/usr/bin/perl use warnings; use strict; my @unsorted_array = ( { ID => 3, Distance => 2.3, RouteDistance => 4.3 }, { ID => 2, Distance => 1.5, RouteDistance => 2.8 }, { ID => 5, Distance => 2.1, RouteDistance => 0 }, { ID => 1, Distance => 1.7, RouteDistance => 2.5 }); my @sorted = sort { if($a->{RouteDistance} == 0 || $b->{RouteDistance} == 0) { return ($a->{Distance} < $b->{Distance}) ? -1 : 1; } elsif ($a->{RouteDistance} == $b->{RouteDistance}) { return 0; } else { return ($a->{RouteDistance} < $b->{RouteDistance}) ? -1 : 1; } } @unsorted_array; foreach my $sorted_row (@sorted) { print "ID: ".$sorted_row->{'ID'}." Distance: ".$sorted_row->{'Distance'}." RouteDistance: ".$sorted_row->{'RouteDistance'}."\n"; }