Sorting object by date

Hey guys,
I need a fast way to sort an array of object by their .time in xml time
scheme. In java,it would be easy using priority queue,is there any thing
similar to priority queue in ruby? Since this is a very small app, it
would be waste of time trying to implement a priority queue.
Thai

On 2/7/07, Thai Le [email protected] wrote:

Hey guys,
I need a fast way to sort an array of object by their .time in xml time
scheme. In java,it would be easy using priority queue,is there any thing
similar to priority queue in ruby? Since this is a very small app, it
would be waste of time trying to implement a priority queue.
Thai

In ruby, you can sort array either by using Array#sort, or
Array#sort_by.
Provided that your .time values have proper <=> operator, you can do:

sorted_array = array_of_objects.sort_by {|o| o.time}

If the <=> operator is not available, you can replace o.time above
with e.g. conversion to timestamp, or implement your own:

sorted_array = array_of_objects.sort {|a,b| …put here the
comparison…}

(the sort by version is faster, as it makes the conversion only once
per array item.

For more details see documentation
(http://ruby-doc.org/core/classes/Array.html#M002235)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs