I am trying to order an object that I "converted" from JSON. I'm trying to use this function that I found on this question, but it does not seem to work in my case:
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) );
});
}
}
And here is my JsFiddle
I want to sort it in 3 different ways:
- chronologically by dict (using "time" field)
- Alphabetically by dict (using "term" field")
- All chronologically (ignore dict separations)
If this object wasn't so nested I would know how to do it. Could I adapt that function?