Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting the contents of an array of hashes 1

Status
Not open for further replies.

Deleted

Technical User
Jul 17, 2003
470
US
I need to sort the contents of an array of hashes by 'distance'.. I have attached the code below.

-----------------------------------------------------------

# Get the list of stores by zip
my $select = "SELECT * FROM stores ORDER BY store_id DESC";
my $db_data = $lib->sql_arrayref($select);

my @temp_array = (); # Loop data

for my $col (@$db_data) {

# // DO SOME PROCESSING AND VALIDATION, ETC, ETC..

push @temp_array, {
name => $col->{name},
street => $col->{street},
city => $col->{city},
state => $col->{state},
zip => $col->{zip},
miles => $col->{distance},
}
}

# Need to sort the contents @temp_array for output, by distance $col->{distance}?

M. Brooks
X Concepts LLC
 
perldoc -f sort

Then try something like this:
Code:
my @sorted_array = sort { $a->{miles} <=> $b->{miles} } @temp_array;

--------------------

Denis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top