无法使用Date.parse()对数组进行排序

时间:2018-03-19 04:16:05

标签: javascript

我想根据时间戳对数组进行排序。我的数组就是这个

[
  {
    who: 'User',
    msg: 'how are you?',
    time: '9:24:19'
  },
  {
    who: 'Bot',
    msg: 'I am fine. Thank you. How are you?',
    time: '9:24:30'
  },
  {
    who: 'User',
    msg: 'hi',
    time: '9:24:36'
  },
  {
    who: 'Bot',
    msg: 'hello i am Joe',
    time: '9:24:47'
  }
]

当我把它作为一个字符串排序时,它工作正常但是当我使用Date.parse(时间)时没有发生任何事情。将它排序为字符串的问题是当我有23:11:8时23:11:36,它认为23:11:36是小的。 怎么办?

这是我的代码

var msg1 = msg.sort((a, b) => {
  console.log('sorting');


  var nameA = a.time; // ignore upper and lowercase
  var nameB = b.time; // ignore upper and lowercase
  console.log(nameA + " " + nameB + " " + Date.parse(nameA) + " " + Date.parse(nameB));
  if (nameA < nameB) {
    console.log('sorting');
    return -1;
  }
  if (nameA > nameB) {
    console.log('sorting');
    return 1;
  }

  // names must be equal
  return 0;
})

4 个答案:

答案 0 :(得分:0)

无法使用默认时间 10:01:44 对数组进行排序,您需要一个合适的时间格式才能解析日期,例如 Mon Mar 19 2018 10:01 :44 以便能够解析

这是一个使用 Date.parse()

对数组进行排序的示例函数

function SortUsersOrder(usersArray){
    let sortedOrder = usersArray.sort(function(a, b){
        let firstDate = Date.parse(a.time);
        let secondDate = Date.parse(b.time);
        if(firstDate < secondDate) return -1;
        if(firstDate > secondDate) return 1;
        return 0;
    });
    return sortedOrder;}
    
    
var sample = [ { who: 'User', msg: 'how are you?', time: 'Mon Mar 19 2018 10:01:44' },
 { who: 'Bot',
   msg: 'I am fine. Thank you. How are you?',
   time: 'Mon Mar 19 2018 10:01:43' },
 { who: 'User', msg: 'hi', time: 'Mon Mar 19 2018 10:01:41' },
  { who: 'Bot', msg: 'hello i am Joe', time: 'Mon Mar 19 2018 10:01:40' } ]


console.log(SortUsersOrder(sample))

答案 1 :(得分:0)

试试这个。

msg.sort((a, b) => {
    var nameA = Date.parse('2018-01-01' + a.time);
    var nameB = Date.parse('2018-01-01' + b.time);
    if (nameA < nameB) { return -1; }
    if (nameA > nameB) { return 1; }
    return 0;
})

Date.parse()需要您插入日期,而不仅仅是时间

答案 2 :(得分:0)

事实上,您不需要Date.parse。你需要进行一些改造才能使你的时代具有可比像这样的东西。这是简化的解决方案。您可能需要考虑AM | PM。

&#13;
&#13;
let msgs = [{
    who: 'User',
    msg: 'how are you?',
    time: '9:06:19'
  },
  {
    who: 'Bot',
    msg: 'I am fine. Thank you. How are you?',
    time: '9:24:50'
  },
  {
    who: 'User',
    msg: 'hi',
    time: '9:24:36'
  },
  {
    who: 'Bot',
    msg: 'hello i am Joe',
    time: '9:24:47'
  }
];

let sorted = msgs.sort((a, b) => {
  let tA = a.time.split(':');
  let tB = b.time.split(':');
  return (tA[0] * 60 * 60 + tA[1] * 60 + tA[2]) > (tB[0] * 60 * 60 + tB[1] * 60 + tB[2]) ? 1 : -1;
});
console.log(sorted);
&#13;
&#13;
&#13;

答案 3 :(得分:0)

第1部分 - 将字符串排序为日期:

只需将任何固定日期附加到其中:

&#13;
&#13;
var time = '9:24:19';
var date = new Date('2018-01-01 ' + time);
console.log(date.toLocaleString())
&#13;
&#13;
&#13;

第2部分 - 按时间排序msgs数组:

(首先显示最新消息)

&#13;
&#13;
var msgs = [{who:'User',msg:'how are you?',time:'9:06:19'},{who:'Bot',msg:'I am fine. Thank you. How are you?',time:'9:24:50'},{who:'User',msg:'hi',time:'9:24:36'},{who:'Bot',msg:'hello i am Joe',time:'9:24:47'}]

var sortedMsgs = msgs.sort((a, b) => {
  var timeA = new Date('2018-01-01 ' + a.time);;
  var timeB = new Date('2018-01-01 ' + b.time);;
  return timeB - timeA;
});

console.log(sortedMsgs);
&#13;
.as-console-wrapper {max-height: 100% !important;top: 0;}
&#13;
&#13;
&#13;

测试:

&#13;
&#13;
var msgs = [{who:'User',msg:'how are you?',time:'9:24:5'},{who:'Bot',msg:'I am fine. Thank you. How are you?',time:'9:24:45'},{who:'User',msg:'hi',time:'9:24:46'},{who:'Bot',msg:'hello i am Joe',time:'9:24:20'}]

var sortedMsgs = msgs.sort((a, b) => {
  var timeA = new Date('2018-01-01 ' + a.time);;
  var timeB = new Date('2018-01-01 ' + b.time);;
  return timeB - timeA;
});

console.log(sortedMsgs.map(s => s.time));
&#13;
&#13;
&#13;