Mailing List Archive


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

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



TLUG,

Just spent the last hour or so going mental trying to apply a sort() on
an object. Turns out it only works on arrays.

Okay, fine, but then it turns out, as far as I can tell by looking
around the net, that there is no sort() on objects, which seems to me a
bit of a miss. I thought objects were supposed to be more dynamic and
flexible.

What I'm actually trying to do is sort a multidimensional object that
was created from JSON data. I found a few examples of how to do that
with an array and the sort() method. Nice, easy to implement examples.

However, sorting the data for an object seems like a nightmare. Or at
least, I haven't been able to find an example that I can parse down to
my level of understanding.

Has anyone encountered this before and found a workable solution?

For more specifics, my object looks like this

toplevel.midlevel.dataIWantToSortBy

"midlevel" is just a number that I use to order the data when I display
it. "dataIWantToSortBy" contains... uh... stuff. For now let's just say
it contains letters for alphabetical sorting.

So What I want to do is turn this:

toplevel.1.dataIWantToSortBy.value = C
toplevel.2.dataIWantToSortBy.value = A
toplevel.3.dataIWantToSortBy.value = B

Into:

toplevel.1.dataIWantToSortBy.value = A
toplevel.2.dataIWantToSortBy.value = B
toplevel.3.dataIWantToSortBy.value = C

I hope that makes sense.

-- 
Dave M G


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links