Fmdpa Posted November 22, 2011 Share Posted November 22, 2011 I'm trying to write an algorithm return an array sorted by a given property in a JSON object. Here's an example of what I want to sort: obj_to_be_sorted = {'four' : {'num' : 4},'one' : {'num' : 1},'three' : {'num' : 3},'two' : {'num' : 2}} sort = function(obj, property, reverse_order) { a = [] for (key in obj) { a.push(key) } return a.sort(a, { /* I think it would involve the sort function... */ }} sorted_array = sort(obj_to_be_sorted, 'num', false); // should return the JSON keys sorted by the 'num' property, i.e. ['one', 'two', 'three', 'four'] I just don't know I would go about doing this. I'm thinking it would be only a few lines of code - very simple - but I'm just not good at writing algorithms yet. I would greatly appreciate any assistance you could give me. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 22, 2011 Share Posted November 22, 2011 Since objects are unsorted, what exactly are you trying to return? Are you trying to return the keys from the object sorted by the num property, even though you're not returning that number? Link to comment Share on other sites More sharing options...
Fmdpa Posted November 22, 2011 Author Share Posted November 22, 2011 Yes. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 22, 2011 Share Posted November 22, 2011 You'll probably need to build the array of keys like you're doing, and then you need to pass a compare function to the array.sort method, similar to PHP's usort. The compare function will need to use the two keys to look up the objects in the global object (the object you start with has to be global for this, or at least in the same scope as the compare function), and compare the properties that you want to compare to decide which should be sorted first. https://developer.mo...ects/Array/sort Link to comment Share on other sites More sharing options...
Fmdpa Posted November 22, 2011 Author Share Posted November 22, 2011 The real scenario is that I have an object (with a similar structure to the one I gave before) that I am building a search function for. The 'num' property would show the relevancy score for that item's key. Here's an example if I was searching for "foo" obj_to_be_sorted = {'foo foo bar foo' : {'relevancy_score' : 3}, // score is 3 because "foo" was matched two times'foo bar' : {'relevancy_score' : 1}, // score is 1 because "foo" was matched once'bar bar' : {'relevancy_score' : 0}, // "foo" wasn't found in either of these so both of their scores are 0'bar' : {'relevancy_score' : 0}} I want to then take the relevancy score and use it to sort the search results. EDIT: just saw your previous post; I'll see what I can come up with. Link to comment Share on other sites More sharing options...
Fmdpa Posted November 23, 2011 Author Share Posted November 23, 2011 This seems to do the trick! sort_results = function(obj, field, reverse) { a = [] for(i in obj) { a.push(i) } return a.sort(function(a, b ) { var A = obj[a][field], B = obj[b][field]; return (A < B ? (typeof A == 'number' && typeof B == 'number' ? A - B : -1) : (A > B ? 1 : 0)) * [-1,1][+!!reverse]; })} Link to comment Share on other sites More sharing options...
justsomeguy Posted November 23, 2011 Share Posted November 23, 2011 I believe the B parameter in the function needs to be lowercase. You're using "b" when accessing the property. Link to comment Share on other sites More sharing options...
Fmdpa Posted November 23, 2011 Author Share Posted November 23, 2011 Oops, I don't know how that happened. You are right. It must have happened somehow when I posted the code here. Fixed. Edit: I don't know what's going on, but the edit won't save the "B" as lowercase! Weird. Link to comment Share on other sites More sharing options...
Ingolme Posted November 23, 2011 Share Posted November 23, 2011 Oops, I don't know how that happened. You are right. It must have happened somehow when I posted the code here. Fixed. Edit: I don't know what's going on, but the edit won't save the "B" as lowercase! Weird.Funny enough, I think I encountered that problem once on this forum. It's some kind of glitch. Link to comment Share on other sites More sharing options...
Fmdpa Posted November 23, 2011 Author Share Posted November 23, 2011 Did it occur before or after the forum update? Link to comment Share on other sites More sharing options...
Ingolme Posted November 23, 2011 Share Posted November 23, 2011 After, I think. But I can't imagine what kind of parseing they're doing to make that happen. Link to comment Share on other sites More sharing options...
Fmdpa Posted November 23, 2011 Author Share Posted November 23, 2011 I know, it's strange. I tried to find a place on IPS's website to file a bug, but I couldn't. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 25, 2011 Share Posted November 25, 2011 I noticed that in Ingolme's response in PHP about using usort. It had a lowercase a and capital B. I bet it's related to emoticon parsing since the B comes before a close paren. Link to comment Share on other sites More sharing options...
Fmdpa Posted November 26, 2011 Author Share Posted November 26, 2011 Ah-ha, that sounds about right. I tried putting a space between the "b" and the parentheses and now it is display correctly. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.