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



Josh Glover writes:
 > On 7 February 2011 09:55, Raymond Wan <rwan.kyoto@example.com> wrote:
 > 
 > > Perhaps languages like Perl and Python can [sort arbitrary lists of objects]
 > 
 > Sure. Here's how to sort arrays of hashes in Perl and Ruby:
 > 
 > Perl:
 > 
 > @array = sort { $a{some_key} <=> $b{some_key} } @array;
 > 
 > Ruby:
 > 
 > array.sort! {|a, b| a['some_key'] <=> b['some_key'] }

Something similar works in other languages, too (ie, all the languages
I know including BrainF!ck -- of course in that last you have to
implement your own hash tables ;-), but Dave's requirement is different.

He doesn't want to sort an array on the value associated with a
specified key in each element of the array, where each such element
happens to be a hash containing that key.  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.

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

OTOH, there are language like SNOBOL (and I believe Tcl) where
"everything *is* a string" (unlike Perl, which decides the type of an
object in a context-dependent way).  Then I would suppose that Dave's
requirement makes sense.



Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links