I have a large JSON returned from a rest service and I need to sort it before I can use it.
Example lines:
[{"Time":"1354233600000","Name":"NAN","TagValue":"0","TagValue2":"0"},
{"Time":"1354234500000","Name":"NAN","TagValue":"0","TagValue2":"0.0020288255172466159"},
{"Time":"1354235400000","Name":"NAN","TagValue":"0","TagValue2":"0.0022446943714048121"},
{"Time":"1354236300000","Name":"NAN","TagValue":"0","TagValue2":"0.00014998416164500384"},
{"Time":"1354237200000","Name":"NAN","TagValue":"0","TagValue2":"0"},
{"Time":"1354238100000","Name":"NAN","TagValue":"0","TagValue2":"0.00015631034628383933"},
{"Time":"1354239000000","Name":"NAN","TagValue":"0","TagValue2":"1.1165024734559951E-05"}
There are about 2000 lines like this. I would like to sort them by time and get something like this:
var restData = { "1354234500000":[
{"Name":"NaN",
"TagValue":"0",
"TagValue2":"someFloat"}
{"Name:"NAN,
"TagValue":"0",
"TagVale":"0"}
],
"aNewUnixTimeStamp":[
{..........}
]};
Is there some magic javascript function that I can use to accomplish this?
You should be aware that since the result is using the timestamps as keys of an object you won’t be able to sort them since objects have no defined order.
Also
.reduce()will need a shim for older browsers. You can use the one provided by MDN.