javascript - javascript - 如何在Object数组中按时间排序?

我有一个对象数组,如


[{


 time:"13:20",


 key2:"",


 key3:""


}, {


 time:"13:40",


 key2:"",


 key3:""


}, {


 time:"04:20",


 key2:"",


 key3:""


}]



我想让他们按照时间排序。 我对momentjs做了一些研究但没有发现有用的东西。

我所面临的主要问题是如何将时间转换为字符串格式,以便进行排序。 任何形式的帮助将不胜感激。谢谢。

时间:

你为所有时间都做了同样的事情。 然后用 getTime() 对比一下


let arr = [{


 time:"13:20",


 key2:"",


 key3:""


}, {


 time:"13:40",


 key2:"",


 key3:""


}, {


 time:"04:20",


 key2:"",


 key3:""


}]



const toTime = (t) => new Date(`December 17, 1995 ${t}`).getTime();



arr.sort((a,b) => toTime(a.time) - toTime(b.time))



console.log(arr)

你可以得到给定时间的分钟,并按这个值排序。


const getMinutes = s => s.split(':').reduce((h, m) => h * 60 + m);



var array = [{ time:"13:20", key2:"", key3:"" }, { time:"13:40", key2:"", key3:"" }, { time:"04:20", key2:"", key3:"" }];



array.sort((a, b) => getMinutes(a.time) - getMinutes(b.time));



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

你可以采用以下方法对它的进行排序:

逻辑

  • 创建日期对象并添加所需的时间值。
  • 根据这些日期值对它的进行排序。

基于日期的方法的好处是它将处理所有时间值案例。 我已经更新了数据以获得第二个值以进行演示


var data = [{


 time:"13:20",


 key2:"",


 key3:""


}, {


 time:"13:40",


 key2:"",


 key3:""


}, {


 time:"04:20:10",


 key2:"",


 key3:""


}, {


 time:"04:20:20",


 key2:"",


 key3:""


}];



function getAddedTimeValue(time) {


 const parts = [...time.split(':'), 0, 0, 0, 0].slice(0, 4);


 const date = new Date();


 date.setHours.apply(date, parts);


 return date.getTime();


}



data.sort((a, b) => getAddedTimeValue(a.time) - getAddedTimeValue(b.time));



//For descending:


//data.sort((a, b) => getAddedTimeValue(b.time) - getAddedTimeValue(a.time));



console.log(data)

如果时间字符串采用 HH:MM 格式,则可以使用 String.prototype.localeCompare() 进行字典排序:


const data = [


 { time: '13:20', key2: '', key3: '' },


 { time: '13:40', key2: '', key3: '' },


 { time: '04:20', key2: '', key3: '' },


 { time: '23:03', key2: '', key3: '' }


];



const ascending = [...data].sort((a, b) => a.time.localeCompare(b.time));


const descending = [...data].sort((a, b) => b.time.localeCompare(a.time));



console.log(JSON.stringify(ascending));


console.log(JSON.stringify(descending));

如果时间字符串不遵循这里格式,则 IE 。 可以指定为H,HH,HH:M,HH:MM .. 然后,你可以使用 RegExp.prototype.exec() 解析字符串:


const data = [


 { time: '13:20', key2: '', key3: '' },


 { time: '13:40', key2: '', key3: '' },


 { time: '04:20', key2: '', key3: '' },


 { time: ' 4:25', key2: '', key3: '' },


 { time: '23:03', key2: '', key3: '' },


 { time: '14: 3', key2: '', key3: '' },


 { time: ' 2 ', key2: '', key3: '' },


];



const hourMinutes = str =>/s*(?<hh>d*)s*:?s*(?<mm>d*)s*/.exec(str);


const toMinutes = ({ hh = 0, mm = 0 }) => (+hh) * 60 + (+mm);


const toTime = ({ time }) => toMinutes(hourMinutes(time).groups);



const ascending = [...data].sort((a, b) => toTime(a) - toTime(b));


const descending = [...data].sort((a, b) => toTime(b) - toTime(a));



console.log(JSON.stringify(ascending));


console.log(JSON.stringify(descending));
...