JSFiddle - React, Tailwind, and code Playground
JavaScript
function sortJsonArrayByProperty(objArray, prop, direction){
if (arguments.length<2) throw new Error("sortJsonArrayByProp requires 2 arguments");
var direct = arguments.length>2 ? arguments[2] : 1; //Default to ascending
if (objArray && objArray.constructor===Array){
var propPath = (prop.constructor===Array) ? prop : prop.split(".");
objArray.sort(function(a,b){
for (var p in propPath){
if (a[propPath[p]] && b[propPath[p]]){
a = a[propPath[p]];
b = b[propPath[p]];
}
}
// convert numeric strings to integers
a = a.match(/^\d+$/) ? +a : a;
b = b.match(/^\d+$/) ? +b : b;
return ( (a < b) ? -1*direct : ((a > b) ? 1*direct : 0) );
});
}
}
var obj = JSON.parse('{"saved":[{"dict":"English","terms":[{"term":"car","rowId":"3487","time":"1384773838069"},{"term":"dog","rowId":"443","time":"1384776129957"},{"term":"plane","rowId":"16171","time":"1384776168253"},{"term":"bread","rowId":"127564","time":"1384959605196"}]},{"dict":"French","terms":[{"term":"Monsieur","rowId":"934","time":"1384862250130"},{"term":"Croissant","rowId":"13612","time":"1384900161187"},{"term":"suis","rowId":"942","time":"1384900437068"}]}]}');
console.log(obj);
console.log(sortJsonArrayByProperty(obj, 'terms.time'));