4

I have a Array Containing Unix time stamps and i want to sort that array any one know how to do it? or maybe converting it to simple date format and then sorting them any way?

Please also give example

I have tried this but didn't work Jquery - Fastest way to sort an array by timestamp

My Code :

//where rawutimes is unsorted unix time array
console.log(rawutimes);


 // output ["1423857905", "1423611874", "1422953913", "1423499856", "1423502234", "1423502884", "1423503754", "1423510832", "1423511445", "1423514134", "1423358619", "1423583825", "1423713297", "1423713401", "1423735601", "1423772523", "1423768351", "1423817039", "1423166917", "1423446473", "1423446761", "1423835014", "1423858124", "1423857905", "1423855048", "1423852400", "1423852313", "1423852210", findIndex: function, find: function]

thesorted = rawutimes.sort(function(x, y){
    return x.timestamp - y.timestamp;
});

//printing the sorted unix time array       
console.log(thesorted);

//Output : ["1423857905", "1423852210", "1422953913", "1423499856", "1423502234", "1423502884", "1423503754", "1423510832", "1423511445", "1423514134", "1423358619", "1423583825", "1423713297", "1423713401", "1423611874", "1423772523", "1423768351", "1423817039", "1423166917", "1423446473", "1423446761", "1423835014", "1423858124", "1423857905", "1423855048", "1423852400", "1423852313", "1423735601", findIndex: function, find: function]

//Converted to Readable date format to confirm if it's sort or not?
rtimes = [];

for (var i = 0; i < rawutimes.length; i++) {
    rtime = timeConverter(thesorted[i]);
    rtimes.push(rtime);
};

//output 


//["14,Feb 2015 1:5:5", "13,Feb 2015 23:30:10", "3,Feb 2015 13:58:33", "9,Feb 2015 21:37:36", "9,Feb 2015 22:17:14", "9,Feb 2015 22:28:4", "9,Feb 2015 22:42:34", "10,Feb 2015 0:40:32", "10,Feb 2015 0:50:45", "10,Feb 2015 1:35:34", "8,Feb 2015 6:23:39", "10,Feb 2015 20:57:5", "12,Feb 2015 8:54:57", "12,Feb 2015 8:56:41", "11,Feb 2015 4:44:34", "13,Feb 2015 1:22:3", "13,Feb 2015 0:12:31", "13,Feb 2015 13:43:59", "6,Feb 2015 1:8:37", "9,Feb 2015 6:47:53", "9,Feb 2015 6:52:41", "13,Feb 2015 18:43:34", "14,Feb 2015 1:8:44", "14,Feb 2015 1:5:5", "14,Feb 2015 0:17:28", "13,Feb 2015 23:33:20", "13,Feb 2015 23:31:53", "12,Feb 2015 15:6:41"]


//result array not sorted

Thanks

4
  • Could you perhaps post the code you tried? It might just be a simple error on your part. Commented Feb 13, 2015 at 20:01
  • [1,2,3].sort(function(a,b){return a-b;}); (numbers are dates are the same as seen from sort() ) Commented Feb 13, 2015 at 20:01
  • Post your code first what you have tried Commented Feb 13, 2015 at 20:06
  • I added the code please take a look! Commented Feb 13, 2015 at 20:21

2 Answers 2

10

You have a list of strings, but use .timestamp in the sort function. Just remove it:

rawutimes = ["1423857905", "1423611874", "1422953913", "1423499856", "1423502234", "1423502884", "1423503754", "1423510832", "1423511445", "1423514134", "1423358619", "1423583825", "1423713297", "1423713401", "1423735601", "1423772523", "1423768351", "1423817039", "1423166917", "1423446473", "1423446761", "1423835014", "1423858124", "1423857905", "1423855048", "1423852400", "1423852313", "1423852210"]

thesorted = rawutimes.sort(function(x, y){
    return x - y;
});

rtimes = thesorted.map(function(x) {
  return new Date(x * 1000);
});

document.write(rtimes.join("<br>"))

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

Comments

0

Try this, perfectly sorted, checked in Chromium browser and Firefox:

<!DOCTYPE html>
<html>
<head>
<script>
var rawutimes = ["1423857905", "1423611874", "1422953913", "1423499856", "1423502234", "1423502884", "1423503754", "1423510832", "1423511445", "1423514134", "1423358619", "1423583825", "1423713297", "1423713401", "1423735601", "1423772523", "1423768351", "1423817039", "1423166917", "1423446473", "1423446761", "1423835014", "1423858124", "1423857905", "1423855048", "1423852400", "1423852313", "1423852210"]
console.log("Raw unix timestamps: " + rawutimes);
var formatted = [];
for (var i = 0; i < rawutimes.length; ++i) {
    var d = new Date(parseInt(rawutimes[i]) * 1000);
    formatted.push(d);
}
thesorted = formatted.sort(function (x, y) {
        return y - x;
        });
console.log("Formatted: " + thesorted);
</script>
</head>
<body>
</body>
</html>

4 Comments

This is not working i'm getting dates like Jan 1970 but actually the timestamp is Feb 2015 not sure what's wrong
Yes I forgot to multiply by 1000 to get milliseconds instead of seconds. Fixed that, please try now.
One more edit: we should sort the final array of date objects instead of the initial timestamps. Fixed that in the code.
Or, as suggested by @georg, just remove '.timestamps' from your sort. :)

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.