Jump to content
Sign in to follow this  
Fmdpa

Sorting Algorithm With A Json Object

Recommended Posts

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.

Edited by Fmdpa

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Edited by Fmdpa

Share this post


Link to post
Share on other sites

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];	})}

Edited by Fmdpa

Share this post


Link to post
Share on other sites

I believe the B parameter in the function needs to be lowercase. You're using "b" when accessing the property.

Share this post


Link to post
Share on other sites

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.

Edited by Fmdpa

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

After, I think. But I can't imagine what kind of parseing they're doing to make that happen.

Share this post


Link to post
Share on other sites

I know, it's strange. I tried to find a place on IPS's website to file a bug, but I couldn't.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Ah-ha, that sounds about right. I tried putting a space between the "b" and the parentheses and now it is display correctly.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...