Skip to content

Instantly share code, notes, and snippets.

@simsam7
Created May 21, 2014 20:11
Show Gist options
  • Save simsam7/99331e6e3b9eab48aa8c to your computer and use it in GitHub Desktop.
Save simsam7/99331e6e3b9eab48aa8c to your computer and use it in GitHub Desktop.
JSON Sort Examples
//some close-to-real JSON data to play with
var data = {
"result": [
{
"rname": "Auntie Cora's Bakery & Restaurant",
"rcontent": [
{
"raddress": "18190 Old Town Front St Tem"
},
{
"rphone": "695-4992"
},
{
"rlatitude": "33.491698669433594",
"rlongitude": "-227.24791633056",
"rdistance": "3"
}
]
},
{
"rname": "Goldilocks Bakery & Restaurants",
"rcontent": [
{
"raddress": "17480 Ynez Rd Tem"
},
{
"rphone": "694-8880"
},
{
"rlatitude": "33.507480612337892",
"rlongitude": "-227.25036773682",
"rdistance": "1.2"
}
]
},
{
"rname": "Marie Callender's Restaurant & Bakery",
"rcontent": [
{
"raddress": "19363 Rancho Calif Rd Tem"
},
{
"rphone": "699-9339"
},
{
"rlatitude": "33.501479553111656",
"rlongitude": "-227.26693225134",
"rdistance": "2.9"
}
]
},
{
"rname": "BJ's Restaurant & Brewhouse",
"rcontent": [
{
"raddress": "16500 Ynez Rd Tem"
},
{
"rphone": "151-8370"
},
{
"rlatitude": "33.507480612337892",
"rlongitude": "-227.25036773682",
"rdistance": "1.2"
},
{
"raddress": "16500 Ynez Rd Tem"
},
{
"rphone": "151-8370"
},
{
"rlatitude": "33.507480612337892",
"rlongitude": "-227.25036773682",
"rdistance": "0.2"
}
]
},
{
"rname": "Simply Sharon's Restaurant",
"rcontent": [
{
"raddress": "17464 Jefferson Ave Tem"
},
{
"rphone": "308-0026"
},
{
"rlatitude": "33.516932761695323",
"rlongitude": "-227.27128027578",
"rdistance": "0.3"
}
]
},
{
"rname": "Jersey's Pizza & Italian Restaurant",
"rcontent": [
{
"raddress": "40557 Cal Oaks Rd Murrieta"
},
{
"rphone": "462-4444"
},
{
"rlatitude": "33.563313974609375",
"rlongitude": "-227.27935943603",
"rdistance": "1.4"
}
]
},
{
"rname": "Arigato Japanese Restaurant",
"rcontent": [
{
"raddress": "42925 Motor Car Pkwy Tem"
},
{
"rphone": "676-8090"
},
{
"rlatitude": "33.527210362318215",
"rlongitude": "-227.25573883056",
"rdistance": "2.5"
}
]
}
]
};
//we want to add the 'closest' distance into the existing data, let's go look for it
for (i = 0; i < data.result.length; i++) {
var name = data.result[i].rname;
console.log("name: " + name);
cl = 99999; //init closest
//there might be more than one rdistance, get the closest, we want to sort on it
for (c = 0; c < data.result[i].rcontent.length; c++) {
if (data.result[i].rcontent[c].hasOwnProperty('rdistance')) {
var dist = data.result[i].rcontent[c].rdistance;
console.log("dist: " + dist);
if (dist < cl) {
cl = dist;
}
}
if (cl != 99999) {
console.log("cl: " + cl);
data.result[i].closest = cl;
}
}
}
console.log("Output: " + JSON.stringify(data.result));
//sort the data
data.result.sort(function(obj1, obj2) {
return obj1.closest - obj2.closest; //sort on the distance
// return obj1.rname.localeCompare(obj2.rname); //sort on the name
});
console.log("Sorted Output: " + JSON.stringify(data.result));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment