Jump to content

Sorting Algorithm With A Json Object


Fmdpa

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.

Link to comment
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

Link to comment
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.

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

Link to comment
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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...