sorting object by date

T

Thai Le

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
 
J

Jan Svitok

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,232
Messages
2,571,168
Members
47,803
Latest member
ShaunaSode

Latest Threads

Top