JavaScript - Sort an Object by an Inner Objects Value
The end result is an array of 2D objects in order by the sort key
Example:
data = {//This is example data to test the function and show that it works
'a':{'k1':'P','k2':'blue'},
'b':{'k1':'Aaa','k2':'red'},
'c':{'k1':'Z','k2':'green'},
'd':{'k1':'O','k2':'yellow'},
'e':{'k1':'BZyetf','k2':'brown'},
'f':{'k1':'BA','k2':'black'},
'g':{'k1':'BA','k2':'cyan'}
}
The above JSON object has an inner object with multiple elements. The keys to the inner object are named k1 and k2. In the example code, the entire object is sorted by the inner objects values associated with key k1.
This code does not use a formula in the sort function, E.g. array.sort(function(){})
In my process, the sort operation is done on a 1D array, which makes the actual sort operation faster and simplier. Other strategies used to sort JSON data on an inner object's value, first put ALL of the data into an array.
The first step in my process does two things simultaneously:
- Maps the sort values to the sort values' associated data
- Creates an array with the sort values.
One difference in my process, is that it puts very limited data into the array that has the sort() method applied to it. So, again, even though my process intially creates an array, the array contains only the sort values. The next difference, is that my process maps each sort value to the element that contains the sort value. See examples below:
Example of new object: {'P':{'a':{'k1':'P','k2':'blue'}},'Aaa':'b':{'k1':'Aaa','k2':'red'},etc.}
Example of array: ['P','Aaa','Z','O','BZyetf','BA','BA']
Creating an object keyed on the sort values, allows the data associated with each sort value to be retrieved easily. The array of sort values is used to retrieve the data back in sorted order by looping through the array of sorted values.
The second, and very easy step in the process is to sort the array of sort values.
arrOfValuesToSort.sort();//Sort the 1 dimensional array of values
The third and last step in the process, is to loop through the array of sorted values and using each sorted value as the key, retrieve the data associated with that sorted value an put it into a final array.
I have not seen any strategy that uses the same process to sort a JSON object by the value of an inner object.