View Single Post
  #4 (permalink)  
Old December 5th, 2006, 09:25 AM
ciderpunx ciderpunx is offline
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Most languages have built in sorting, which works well in the majority of cases - you should use these. If keeping the information in a sorted order is important you may want to consider using a data structure other than an array.

Anyhow, here's a couple of snippets that find the lowest number in an array.

Here's a perl version:
Code:
my @unsorted = (34, 69, 12, 67, 21, 9, 78, 478327);
my @sorted = sort {$a <=> $b} @unsorted;
print "$sorted[0]\n";
Here's a ruby version:
Code:
arr=[34, 69, 12, 67, 21, 9, 78, 478327]
p arr.sort.first
Cheers

--
Don't Stand on your head - you'll get footprints in your hair
                                           http://charlieharvey.org.uk
                                              http://charlieharvey.com
Reply With Quote