Sort Json by 2 keys.


function sortJSONBy2Keys(a,b,c){var d=function(b,c){return b>c?1:b<c?-1:0};return a.sort(function(a,e){var f=a[b],g=e[b],h=a[c],i=e[c];return d([d(f,g),d(h,i)],[d(g,f),d(i,h)])})}

var sortedJson = sortJSONBy2Keys(originalJson,'key1','key2');

 

Example: https://jsfiddle.net/1934z53s/

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s