Mailing List Archive
tlug.jp Mailing List tlug archive tlug 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
- Date: Mon, 07 Feb 2011 22:50:35 +0900
- From: Dave M G <dave@example.com>
- Subject: Re: [tlug] [Javascript] Shouldn't there be a sort option on objects
- References: <4D4E75AB.5060703@example.com> <87aai91c4o.fsf@example.com> <4D4F65F2.1060000@example.com> <87sjw0z3g8.fsf@example.com>
- User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101208 Thunderbird/3.1.7
Stephen, Josh, Fredric, Raymond, Thanks guys for your explanations. Stephen's "between a banana and red" explanation really helped me get a hold on the concept that objects are not happy when one tries to sort them. So... I'm down with the idea that what I need to do is be dealing with a simple array, and not an object. This should be possible, as the data I'm dealing with is data that has been sent from a server in JSON format. I thought I could maybe stop Javascript from thinking of it as an object and save it as an array right off, but it seems to be entirely automatic that it's made into an object. I think I should just be really specific about what I'm trying to do. The code I use to send and receive JSON data looks like this: $.post('jsonhandler.php', {JSON: JSON.stringify(data)}, checkResult, "json"); "checkresult" is the function that does stuff with the JSON data that comes back from the server, and all it does is this (well, right now I'm just testing things): function checkResult(JSONdata) { console.log(JSONdata); } So, at this point, I haven't acted on the JSON data or anything, but when I'm checking what I've got in the console (I use Firebug in Firefox), it says in capital letters that the data is an OBJECT. The contents of that data look like this OBJECT JSONdata 1 displayName: "Dude1" email: "dude1@example.com <mailto:dude1@example.com>" lastActive: 1296980700 level: 57 timeout: 12969932837 2 displayName: "Dude2" email: "dude2@example.com <mailto:dude2@example.com>" lastActive: 1296983456 level: 28 timeout: 12969937382 3 displayName: "Dude3" email: "dude3@example.com <mailto:dude3@example.com>" lastActive: 1296980749 level: 99 timeout: 129699323459 The numbers, 1, 2, and 3, are the values stored in the first level of the object. They are just represent the order in which the values were constructed when the data was created by the server (in PHP). What I want to be able to do is reorder these based on one of the values of the second level of the object. So, for example, if I sorted (descending) on the "level" key, it would be like this: OBJECT JSONdata 1 displayName: "Dude2" email: "dude2@example.com <mailto:dude2@example.com>" lastActive: 1296980749 level: 99 timeout: 129699323459 2 displayName: "Dude1" email: "dude1@example.com <mailto:dude1@example.com>" lastActive: 1296980700 level: 57 timeout: 12969932837 3 displayName: "Dude3" email: "dude3@example.com <mailto:dude3@example.com>" lastActive: 1296983456 level: 28 timeout: 12969937382 If I can get JSON to look at this as an array, not an object, I think I can do something like this: function sortBylevel(a, b) { var x = a.level; var y = b.level; return ((x < y) ? -1 : ((x > y) ? 1 : 0)); } JSONdata.sort(sortByLevel); However, I've searched all over Google, and I can't seem to find any way of either making sure the JSON is made into an array in the first place, or converting it from an object into an array after the fact. Everyone seems to want to convert from objects to JSON and all discussion about the other way around seems to talk about it like it's so obvious or automatic that it doesn't merit mention. Can anyone break it down to me on my lower-than-noob level? -- Dave M G
- Follow-Ups:
- Re: [tlug] [Javascript] Shouldn't there be a sort option on objects
- From: Raymond Wan
- Re: [tlug] [Javascript] Shouldn't there be a sort option on objects
- From: Dave M G
- References:
- [tlug] [Javascript] Shouldn't there be a sort option on objects
- From: Dave M G
- [tlug] [Javascript] Shouldn't there be a sort option on objects
- From: Stephen J. Turnbull
- Re: [tlug] [Javascript] Shouldn't there be a sort option on objects
- From: Dave M G
- Re: [tlug] [Javascript] Shouldn't there be a sort option on objects
- From: Stephen J. Turnbull
Home | Main Index | Thread Index
- Prev by Date: Re: [tlug] [Javascript] Shouldn't there be a sort option on objects
- Next by Date: Re: [tlug] [Javascript] Shouldn't there be a sort option on objects
- Previous by thread: Re: [tlug] [Javascript] Shouldn't there be a sort option on objects
- Next by thread: Re: [tlug] [Javascript] Shouldn't there be a sort option on objects
- Index(es):
Home Page Mailing List Linux and Japan TLUG Members Links