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

时间:2016-05-27 10:05:48

标签: javascript jquery html

我有一个像这样的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"]
 }

我想对这些数据进行排序根据时间和日期,在查看堆栈后,我发现了这个:

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

但这并没有做任何工作,而是说jsonData.sort不是函数

4 个答案:

答案 0 :(得分:2)

sort() 方法只能应用于数组,提供的数据是对象。

如果您只想要排序的索引数组

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)

或者,如果您想根据时间和日期将其转换为已排序的数组,请执行以下操作:

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)

如果您想基于对象键重新索引,那么使用排序对象数组

执行类似的操作

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)

答案 1 :(得分:1)

您无法对对象进行排序,但可以对对象键数组进行排序

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)

然后您可以使用该排序数组来获取对象值DEMO

答案 2 :(得分:1)

首先,它不是一个数组,它是一个对象。您需要先将其转换为数组。

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

<强>样本

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

答案 3 :(得分:0)

您的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"
    }
}

然后,你循环遍历每个事件,然后按新事件中的event_date(如果date等于+ event_time)对它们进行排序。