Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tlug] [Javascript] Shouldn't there be a sort option on objects



On 8 February 2011 00:39, Stephen J. Turnbull <turnbull@example.com> wrote:

> He wants to reorder the
> keys of the hash so that mapping over the hash in its "natural" order
> of keys produces the values in the desired order.

The easiest way to do that (in Ruby--Perl syntax is basically the
same, Javascript syntax shouldn't be too different) is simply:

my_object.keys.sort{|a,b| a[:some_key] <=> b[:some_key]}.map {...}

> Of the languages so far mentioned, I would consider Perl most likely
> to provide such a facility (but I bet it doesn't).

Ruby has a hash that keeps the keys FIFO ordered, if that makes sense.
The first key you put in is the first key that
GoogleOnlyKnowsTheNameOfTheHashClass#keys will return.

-- 
Cheers,
Josh


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links