简体   繁体   English

根据时间和日期对对象数组进行排序

[英]Sorting An Array Of Objects Based On Time And Date

I have a json object like this: 我有一个像这样的json对象:

{
    "1":["Test Event 1","5","interview","08:30:00","2016-05-28","1"],
    "2":["Test 2","2","Lesser Important Items","08:30:00","2016-05-27","0"],
    "3":["Test Event 4","5","meeting","08:30:00","2016-06-12","1"],
    "4":["","0","Lesser Important Items","08:30:00","2016-06-12","0"],
    "5":["","0","Lesser Important Items","08:30:00","2016-06-12","0"],
    "6":["Test Event 3","1","interview","19:30:00","2016-05-29","1"]
 }

I want to sort this data On the basis of the time and date, after looking on stack, i found this : 我想根据时间和日期对数据进行排序,在堆栈上查找后,我发现了这一点:

jsonData.sort(function(a, b) {
    return a.time - b.time;
})

but this ain't doing any job and instead saying that jsonData.sort is not a function 但这没有做任何工作,而是说jsonData.sort不是一个函数

The sort() method can only apply applied to array, the provided data is an object. sort()方法只能应用于数组,所提供的数据是一个对象。

If you just want the sorted index array 如果只需要排序的索引数组

 var data = { "1": ["Test Event 1", "5", "interview", "08:30:00", "2016-05-28", "1"], "2": ["Test 2", "2", "Lesser Important Items", "08:30:00", "2016-05-27", "0"], "3": ["Test Event 4", "5", "meeting", "08:30:00", "2016-06-12", "1"], "4": ["", "0", "Lesser Important Items", "08:30:00", "2016-06-12", "0"], "5": ["", "0", "Lesser Important Items", "08:30:00", "2016-06-12", "0"], "6": ["Test Event 3", "1", "interview", "19:30:00", "2016-05-29", "1"] }; var res = Object.keys(data) // get object keys array .sort(function(a, b) { // sort the key array based on the date and time // convert to date and get difference for sorting return new Date(data[a][4] + ' ' + data[a][3]) - new Date(data[b][4] + ' ' + data[b][3]); }) console.log(res) 


Or if you want to convert it to a sorted array based on the time and date then do something like this, 或者,如果您想根据时间和日期将其转换为排序的数组,请执行以下操作,

 var data = { "1": ["Test Event 1", "5", "interview", "08:30:00", "2016-05-28", "1"], "2": ["Test 2", "2", "Lesser Important Items", "08:30:00", "2016-05-27", "0"], "3": ["Test Event 4", "5", "meeting", "08:30:00", "2016-06-12", "1"], "4": ["", "0", "Lesser Important Items", "08:30:00", "2016-06-12", "0"], "5": ["", "0", "Lesser Important Items", "08:30:00", "2016-06-12", "0"], "6": ["Test Event 3", "1", "interview", "19:30:00", "2016-05-29", "1"] }; var res = Object.keys(data) // get object keys array .sort(function(a, b) { // sort the key array based on the date and time // convert to date and get difference for sorting return new Date(data[a][4] + ' ' + data[a][3]) - new Date(data[b][4] + ' ' + data[b][3]); }).map(function(v) { // use map to generate the object array return data[v] // get object from the data }); console.log(res) 


In case if you want to re-index based the object keys then do something like this using sorted object array 如果要基于对象键重新索引,请使用已排序的对象数组执行以下操作

 var data = { "1": ["Test Event 1", "5", "interview", "08:30:00", "2016-05-28", "1"], "2": ["Test 2", "2", "Lesser Important Items", "08:30:00", "2016-05-27", "0"], "3": ["Test Event 4", "5", "meeting", "08:30:00", "2016-06-12", "1"], "4": ["", "0", "Lesser Important Items", "08:30:00", "2016-06-12", "0"], "5": ["", "0", "Lesser Important Items", "08:30:00", "2016-06-12", "0"], "6": ["Test Event 3", "1", "interview", "19:30:00", "2016-05-29", "1"] }; Object.keys(data) // get object keys array .sort(function(a, b) { // sort the key array based on the date and time // convert to date and get difference for sorting return new Date(data[a][4] + ' ' + data[a][3]) - new Date(data[b][4] + ' ' + data[b][3]); }).map(function(v) { // use map to generate the object array return data[v] // get object from the data }).forEach(function(v, i) { data[i + 1] = v; // update based on the sorted array }) console.log(data) 

You cant sort object but you can sort array of object keys 您不能对对象进行排序,但是可以对对象键的数组进行排序

 var obj = { "1": ["Test Event 1", "5", "interview", "08:30:00", "2016-05-28", "1"], "2": ["Test 2", "2", "Lesser Important Items", "08:30:00", "2016-05-27", "0"], "3": ["Test Event 4", "5", "meeting", "08:30:00", "2016-06-12", "1"], "4": ["", "0", "Lesser Important Items", "08:30:00", "2016-06-12", "0"], "5": ["", "0", "Lesser Important Items", "08:30:00", "2016-06-12", "0"], "6": ["Test Event 3", "1", "interview", "19:30:00", "2016-05-29", "1"] } var sort = Object.keys(obj).sort(function(a, b) { return Date.parse(obj[a][4]) - Date.parse(obj[b][4]) || Date.parse(obj[a][3].replace(/:/g, ' ')) - Date.parse(obj[b][3].replace(/:/g, ' ')); }) console.log(sort) 

Then you can use that sorted array to get object values DEMO 然后,您可以使用该排序数组来获取对象值DEMO

First of all, it is not an array it is an object. 首先,它不是数组,而是对象。 You need to convert this to array first. 您需要先将其转换为数组。

var obj = {
    "1":["Test Event 1","5","interview","08:30:00","2016-05-28","1"],
    "2":["Test 2","2","Lesser Important Items","08:30:00","2016-05-27","0"],
    "3":["Test Event 4","5","meeting","08:30:00","2016-06-12","1"],
    "4":["","0","Lesser Important Items","08:30:00","2016-06-12","0"],
    "5":["","0","Lesser Important Items","08:30:00","2016-06-12","0"],
    "6":["Test Event 3","1","interview","19:30:00","2016-05-29","1"]
 }
var arr = Object.keys(obj).map(function(key){ return [key, obj[key]] });

Now you can sort it on the date and time as 现在您可以按日期和时间对其进行排序

arr.sort(function(a,b){ 
  var da = a[1][4], ta = a[1][3], db = b[1][4], tb = b[1][3];  
  da = da.split("-").map(function(val){ return parseInt(val,10); }); 
  ta = ta.split(":").map(function(val){ return parseInt(val,10); });
  db = db.split("-").map(function(val){ return parseInt(val,10); }); 
  tb = tb.split(":").map(function(val){ return parseInt(val,10); });

  var dateA = new Date( da[0], da[1]-1, da[2], ta[0], ta[1], ta[2] );
  var dateB = new Date( db[0], db[1]-1, db[2], tb[0], tb[1], tb[2] );
  return dateA.getTime() -dateB.getTime();
})

DEMO 演示

 var obj = { "1":["Test Event 1","5","interview","08:30:00","2016-05-28","1"], "2":["Test 2","2","Lesser Important Items","08:30:00","2016-05-27","0"], "3":["Test Event 4","5","meeting","08:30:00","2016-06-12","1"], "4":["","0","Lesser Important Items","08:30:00","2016-06-12","0"], "5":["","0","Lesser Important Items","08:30:00","2016-06-12","0"], "6":["Test Event 3","1","interview","19:30:00","2016-05-29","1"] } var arr = Object.keys(obj).map(function(key){ return [key, obj[key]] }); arr.sort(function(a,b){ var da = a[1][4], ta = a[1][3], db = b[1][4], tb = b[1][3]; da = da.split("-").map(function(val){ return parseInt(val,10); }); ta = ta.split(":").map(function(val){ return parseInt(val,10); }); db = db.split("-").map(function(val){ return parseInt(val,10); }); tb = tb.split(":").map(function(val){ return parseInt(val,10); }); var dateA = new Date( da[0], da[1]-1, da[2], ta[0], ta[1], ta[2] ); var dateB = new Date( db[0], db[1]-1, db[2], tb[0], tb[1], tb[2] ); return dateA.getTime() -dateB.getTime(); }) console.log(arr); 

Your json object is not well formated, it should be something like : 您的json对象格式不正确,应该类似于:

{
    {
       "even_id":"1",
       "event_name":"Test Event",
       "event_watever":"1",
       "event_time":"08:30:00",
       "event_date":"2016-05-28"
    },
    {
       "even_id":"2",
       "event_name":"Test Event2",
       "event_watever":"5",
       "event_time":"08:30:00",
       "event_date":"2016-05-28"
    }
}

And, with that, you loop over each event and you sort them by event_date ( + event_time if date are equals) in a new Array. 而且,这样,您就可以遍历每个事件,并在新数组中按event_date(如果日期等于,则为event_time)对它们进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM