1

i have a JS object JSON. i need to sort this based on time

I have done some conversion on time and converted SqlTime stamp to JS Date time.

Need to sort the below based on time

[
{
"file_name":"150412-001070",
"date_time":"2015-07-21T13:11:55.000Z",
"polpospercent":68.95,"polnegpercent":31.05,
"Anger":6.58,
"Surprise":32.87,
"Sadness":32.87,
"Joy":4.59,
"Disgust":13.84,
"Fear":9.26,
"file_ts":"2014-04-26T22:03:00.000Z"
},

{"file_name":"150412-001070",
"date_time":"2015-07-21T13:11:55.000Z",
"polpospercent":68.95,"polnegpercent":31.05,
"Anger":6.58,
"Surprise":32.87,
"Sadness":32.87,
"Joy":4.59,
"Disgust":13.84,
"Fear":9.26,
"file_ts":"2014-05-26T22:03:00.000Z"
}
]
Jayesh Goyani
  • 10,535
  • 11
  • 24
  • 48
Siddharth
  • 575
  • 1
  • 6
  • 24
  • 1
    Here is ur Answer http://stackoverflow.com/questions/10123953/sort-javascript-object-array-by-date – Alireza Masali Jul 23 '15 at 07:28
  • @Jayesh Goyani you shouldn't have removed the AngularJS tag ... there might very well be valid AngularJS answers to this thread – devnull69 Jul 23 '15 at 07:49

7 Answers7

1

Use the filter function on your array :

var a = [
{
"file_name":"150412-001070",
"date_time":"2013-07-21T13:11:55.000Z",
"polpospercent":68.95,"polnegpercent":31.05,
"Anger":6.58,
"Surprise":32.87,
"Sadness":32.87,
"Joy":4.59,
"Disgust":13.84,
"Fear":9.26,
"file_ts":"2014-04-26T22:03:00.000Z"
},

{"file_name":"150412-001070",
"date_time":"2014-07-21T14:11:55.000Z",
"polpospercent":68.95,"polnegpercent":31.05,
"Anger":6.58,
"Surprise":32.87,
"Sadness":32.87,
"Joy":4.59,
"Disgust":13.84,
"Fear":9.26,
"file_ts":"2014-05-26T22:03:00.000Z"
},

{"file_name":"150412-001070",
"date_time":"2017-07-21T18:11:55.000Z",
"polpospercent":68.95,"polnegpercent":31.05,
"Anger":6.58,
"Surprise":32.87,
"Sadness":32.87,
"Joy":4.59,
"Disgust":13.84,
"Fear":9.26,
"file_ts":"2014-05-26T22:03:00.000Z"
},

{"file_name":"150412-001070",
"date_time":"2010-07-21T13:11:55.000Z",
"polpospercent":68.95,"polnegpercent":31.05,
"Anger":6.58,
"Surprise":32.87,
"Sadness":32.87,
"Joy":4.59,
"Disgust":13.84,
"Fear":9.26,
"file_ts":"2014-05-26T22:03:00.000Z"
}
]

var b = a.sort(function(x,y){
  return new Date(x.date_time).getTime() - new Date(y.date_time).getTime();
})

console.log(b);
Thom-x
  • 841
  • 1
  • 6
  • 20
1

got some reference

function sort(jsonArray, key){
                if(jsonArray){
                   var sortedArray = jsonArray.sort(function(left, right) { 
                                     //array.sort is buit-in function
                       var a = left[key];
                       var b = right[key];
                       if (a !== b) {
                           if (a > b || a === void 0) return 1;
                           if (a < b || b === void 0) return -1;
                       }
                       return 0;
                  });
                  return sortedArray;
                }
            }

it seems to work

Siddharth
  • 575
  • 1
  • 6
  • 24
1

use sort function, ann convert it to js date obj.

var array=[
{
"file_name":"150412-001070",
"date_time":"2015-07-21T13:11:55.000Z",
"polpospercent":68.95,"polnegpercent":31.05,
"Anger":6.58,
"Surprise":32.87,
"Sadness":32.87,
"Joy":4.59,
"Disgust":13.84,
"Fear":9.26,
"file_ts":"2014-04-26T22:03:00.000Z"
},

{"file_name":"150412-001070",
"date_time":"2015-07-21T13:11:55.000Z",
"polpospercent":68.95,"polnegpercent":31.05,
"Anger":6.58,
"Surprise":32.87,
"Sadness":32.87,
"Joy":4.59,
"Disgust":13.84,
"Fear":9.26,
"file_ts":"2014-05-26T22:03:00.000Z"
}
];

console.log(array);
var sortedArray=array.sort(function(a,b){
  return new Date(a.file_ts)- new Date(b.file_ts);
})
console.log(sortedArray);
user3227295
  • 2,088
  • 1
  • 8
  • 16
  • 1
    Converting is not necessary ... the date_time property is in ISO string format which provides "natural" sortability – devnull69 Jul 23 '15 at 07:33
0

You can use sort() for your array providing a custom sort function

yourjsonobject.sort(function(a, b) {
  if (a.date_time < b.date_time) {
    return -1;
  }
  if (a.date_time > b.date_time) {
    return 1;
  }
  return 0;
});
devnull69
  • 15,158
  • 3
  • 44
  • 55
0

Try with this sort function. It works for me!

var files= [
    {
    "file_name":"150412-001070",
    "date_time":"2015-07-21T13:11:55.000Z",
    "polpospercent":68.95,"polnegpercent":31.05,
    "Anger":6.58,
    "Surprise":32.87,
    "Sadness":32.87,
    "Joy":4.59,
    "Disgust":13.84,
    "Fear":9.26,
    "file_ts":"2014-04-26T22:03:00.000Z"
    },
    {
    "file_name":"150412-001070",
    "date_time":"2015-07-21T13:11:55.000Z",
    "polpospercent":68.95,"polnegpercent":31.05,
    "Anger":6.58,
    "Surprise":32.87,
    "Sadness":32.87,
    "Joy":4.59,
    "Disgust":13.84,
    "Fear":9.26,
    "file_ts":"2014-05-26T22:03:00.000Z"
    }
    ];

function sortFiles() {
    files = files.sort(function(a, b) {
        return a["file_ts"]- b["file_ts"];

    });
}
Sapikelio
  • 2,456
  • 2
  • 14
  • 37
0

Just use getTime() method after creating a date object and use filter method. You can use moment as well for date comparision.

But here is what which is simple:-

array.sort(function(a, b){
  return new Date(a.date_time).getTime() < new Date(b.date_time).getTime();
});
Ankit
  • 892
  • 5
  • 12
-2

To order by time you can use the orderBy operator.

The orderBy filter the specific array using an expression.

{{[
    {
        "file_name": "150412-001070",
        "date_time": "2015-07-21T13:11:55.000Z",
        "polpospercent": 68.95,
        "polnegpercent": 31.05,
        "Anger": 6.58,
        "Surprise": 32.87,
        "Sadness": 32.87,
        "Joy": 4.59,
        "Disgust": 13.84,
        "Fear": 9.26,
        "file_ts": "2014-04-26T22:03:00.000Z"
    },
    {
        "file_name": "150412-001070",
        "date_time": "2015-07-21T13:11:55.000Z",
        "polpospercent": 68.95,
        "polnegpercent": 31.05,
        "Anger": 6.58,
        "Surprise": 32.87,
        "Sadness": 32.87,
        "Joy": 4.59,
        "Disgust": 13.84,
        "Fear": 9.26,
        "file_ts": "2014-05-26T22:03:00.000Z"
    }
] | orderBy : '+date_time'}} 

You can pass either a + or a - to force the sort in ascending or descending order.

Endre Simo
  • 10,388
  • 2
  • 34
  • 44