如何对Javascript Unix时间戳数组进行排序

如何对Javascript Unix时间戳数组进行排序,javascript,arrays,sorting,unix,Javascript,Arrays,Sorting,Unix,我有一个包含Unix时间戳的数组,我想对该数组进行排序,有人知道怎么做吗?或者将其转换为简单的日期格式,然后以任何方式对其进行排序 还请举例说明 我试过了,但没用 我的代码: //where rawutimes is unsorted unix time array console.log(rawutimes); // output ["1423857905", "1423611874", "1422953913", "1423499856", "1423502234", "1423502

我有一个包含Unix时间戳的数组,我想对该数组进行排序,有人知道怎么做吗?或者将其转换为简单的日期格式,然后以任何方式对其进行排序

还请举例说明

我试过了,但没用

我的代码:

//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
//其中rawutimes是未排序的unix时间数组
控制台日志(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”、findIndex:function、find:function]
排序=rawutimes.sort(函数(x,y){
返回x.timestamp-y.timestamp;
});
//打印已排序的unix时间数组
console.log(排序);
//输出:[“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]
//转换为可读的日期格式以确认是否排序?
rtimes=[];
对于(变量i=0;i

谢谢,请在Chromium浏览器和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>

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"]
log(“原始unix时间戳:+rawutimes”);
var格式=[];
对于(变量i=0;i
您有一个字符串列表,但在排序函数中使用
.timestamp
。只需将其删除:

rawutimes=[“1423857905”、“1423611874”、“1422953913”、“1423499856”、“1423502234”、“1423502884”、“1423503754”、“1423510832”、“1423511445”、“1423514134”、“1423358619”、“1423583825”、“1423713297”、“1423713401”、“1423735601”、“1423772523”、“1423768351”、“1423817039”、“1423166917”、“14234463”、“1423446761”、“1423835014”、“1423858124”1423857905", "1423855048", "1423852400", "1423852313", "1423852210"]
排序=rawutimes.sort(函数(x,y){
返回x-y;
});
rtimes=sorted.map(函数(x){
返回新日期(x*1000);
});

document.write(rtimes.join(“
”)
您是否可以发布您尝试的代码?这可能只是您的一个简单错误。
[1,2,3].sort(函数(a,b){return a-b;});
(数字是日期,与从sort()中看到的日期相同)首先发布您的代码triedI添加了什么代码请看一看!这不起作用我得到的日期是1970年1月,但实际上时间戳是2015年2月不确定是什么问题我忘了乘以1000得到毫秒而不是秒。修复了,请现在再试一次。再次编辑:我们应该对日期对象的最终数组进行排序而不是初始时间戳。修复了代码中的问题。或者,正如@georg所建议的,只需从排序中删除“.timestamps”。)