1

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?

I Hate Lazy
  • 43,114
  • 10
  • 81
  • 75
baron aron
  • 159
  • 7

3 Answers3

1

Assuming your objects are all neatly organised in an array, you can just call

bigArray.sort(function(a,b) {return a.Time < b.Time ? -1 : 1})
Niet the Dark Absol
  • 301,028
  • 70
  • 427
  • 540
1
var myarray = [{"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"}

var result = myarray.reduce(function(res, obj) {
    if (res.hasOwnProperty(obj.Time) === false) {
        res[obj.Time] = [];
    }
    res[obj.Time].push(obj);
    delete obj.Time;
    return res;
}, {});

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.

I Hate Lazy
  • 43,114
  • 10
  • 81
  • 75
0

you can also use this pligin from jquery

http://archive.plugins.jquery.com/project/sort

$(dataSet).sort("Time", "asc");
COLD TOLD
  • 12,989
  • 3
  • 31
  • 49
  • -1 because it would be lunacy to load a large DOM manipulation library *(and plugins)* just to do simple object manipulation. – I Hate Lazy Dec 01 '12 at 02:56
  • @user1689607 There are about 2000 lines of data hmmmm may be simple object manipulation but with huge data plus why not use something that there to do a job for you – COLD TOLD Dec 01 '12 at 02:58
  • Loading jQuery won't make any of those 2000 lines disappear. Using generalized abstractions just slows things down. – I Hate Lazy Dec 01 '12 at 03:00
  • why unless you write your own sorting you still use designed function do not see any point of what you want to prove – COLD TOLD Dec 01 '12 at 03:04
  • Code written for a specific task will generally outperform code written to handle a larger range of generalized tasks. Anyway the performance is secondary. It makes no sense to load a large DOM manipulation library just to do a simple sort. You wanted to know the reason for the vote, and I've explained it in detail. If you can't understand at this point, I can't help you. – I Hate Lazy Dec 01 '12 at 03:08