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"
}
]
2
  • 1
    Here is ur Answer stackoverflow.com/questions/10123953/… Commented Jul 23, 2015 at 7:28
  • @Jayesh Goyani you shouldn't have removed the AngularJS tag ... there might very well be valid AngularJS answers to this thread Commented Jul 23, 2015 at 7:49

7 Answers 7

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);

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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);

1 Comment

Converting is not necessary ... the date_time property is in ISO string format which provides "natural" sortability
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;
});

Comments

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"];

    });
}

Comments

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();
});

Comments

-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.

4 Comments

Ahem ... this doesn't seem to be an AngularJS question, does it?
Hey why the downvote? It was tagged as an angular question, yes?
I didn't see this tag ... right now it is not tagged like this (any more)
You are right, someone removed the tag ... that was not a good idea

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.