假设我有一个包含几个对象的数组:
var array = [{id: 1, date: Mar 12 2012 10:00:00 AM}, {id: 2, date: Mar 8 2012 08:00:00 AM}];
如何按日期元素排序这个数组,从最接近当前日期和时间的日期下来?请记住,数组可能有许多对象,但为了简单起见,我使用2。
我会使用排序函数和自定义比较器吗?
假设我有一个包含几个对象的数组:
var array = [{id: 1, date: Mar 12 2012 10:00:00 AM}, {id: 2, date: Mar 8 2012 08:00:00 AM}];
如何按日期元素排序这个数组,从最接近当前日期和时间的日期下来?请记住,数组可能有许多对象,但为了简单起见,我使用2。
我会使用排序函数和自定义比较器吗?
当前回答
我推荐GitHub: Array sortBy - sortBy方法的最佳实现,它使用施瓦茨变换
但是现在我们将尝试这种方法。 让我们创建一个方法来排序数组,它能够按某些属性排列对象。
创建排序函数
var sortBy = (function () {
var toString = Object.prototype.toString,
// default parser function
parse = function (x) { return x; },
// gets the item to be sorted
getItem = function (x) {
var isObject = x != null && typeof x === "object";
var isProp = isObject && this.prop in x;
return this.parser(isProp ? x[this.prop] : x);
};
/**
* Sorts an array of elements.
*
* @param {Array} array: the collection to sort
* @param {Object} cfg: the configuration options
* @property {String} cfg.prop: property name (if it is an Array of objects)
* @property {Boolean} cfg.desc: determines whether the sort is descending
* @property {Function} cfg.parser: function to parse the items to expected type
* @return {Array}
*/
return function sortby (array, cfg) {
if (!(array instanceof Array && array.length)) return [];
if (toString.call(cfg) !== "[object Object]") cfg = {};
if (typeof cfg.parser !== "function") cfg.parser = parse;
cfg.desc = !!cfg.desc ? -1 : 1;
return array.sort(function (a, b) {
a = getItem.call(cfg, a);
b = getItem.call(cfg, b);
return cfg.desc * (a < b ? -1 : +(a > b));
});
};
}());
设置无序数据
var data = [
{date: "2011-11-14T17:25:45Z", quantity: 2, total: 200, tip: 0, type: "cash"},
{date: "2011-11-14T16:28:54Z", quantity: 1, total: 300, tip: 200, type: "visa"},
{date: "2011-11-14T16:30:43Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T17:22:59Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T16:53:41Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T16:48:46Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-31T17:29:52Z", quantity: 1, total: 200, tip: 100, type: "visa"},
{date: "2011-11-01T16:17:54Z", quantity: 2, total: 190, tip: 100, type: "tab"},
{date: "2011-11-14T16:58:03Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T16:20:19Z", quantity: 2, total: 190, tip: 100, type: "tab"},
{date: "2011-11-14T17:07:21Z", quantity: 2, total: 90, tip: 0, type: "tab"},
{date: "2011-11-14T16:54:06Z", quantity: 1, total: 100, tip: 0, type: "cash"}
];
使用它
最后,通过“date”属性将数组排列为字符串
//sort the object by a property (ascending)
//sorting takes into account uppercase and lowercase
sortBy(data, { prop: "date" });
如果你想忽略字母大小写,设置"parser"回调:
//sort the object by a property (descending)
//sorting ignores uppercase and lowercase
sortBy(data, {
prop: "date",
desc: true,
parser: function (item) {
//ignore case sensitive
return item.toUpperCase();
}
});
如果你想把"date"字段作为date类型:
//sort the object by a property (ascending)
//sorting parses each item to Date type
sortBy(data, {
prop: "date",
parser: function (item) {
return new Date(item);
}
});
在这里你可以玩上面的例子: jsbin.com/lesebi
其他回答
在纠正JSON之后,这应该为你工作了:
var array = [{id: 1, date:'Mar 12 2012 10:00:00 AM'}, {id: 2, date:'Mar 8 2012 08:00:00 AM'}];
array.sort(function(a, b) {
var c = new Date(a.date);
var d = new Date(b.date);
return c-d;
});
谢谢Ganesh Sanap。按日期字段从旧到新对项目进行排序。使用它
myArray = [{transport: "Air",
load: "Vatican Vaticano",
created: "01/31/2020"},
{transport: "Air",
load: "Paris",
created: "01/30/2020"}]
myAarray.sort(function(a, b) {
var c = new Date(a.created);
var d = new Date(b.created);
return c-d;
});
我只是把上面描述的施瓦兹变换作为一个函数。它接受一个数组,排序函数和一个布尔值作为输入:
function schwartzianSort(array,f,asc){
for (var i=array.length;i;){
var o = array[--i];
array[i] = [].concat(f.call(o,o,i),o);
}
array.sort(function(a,b){
for (var i=0,len=a.length;i<len;++i){
if (a[i]!=b[i]) return a[i]<b[i]?asc?-1:1:1;
}
return 0;
});
for (var i=array.length;i;){
array[--i]=array[i][array[i].length-1];
}
return array;
}
function schwartzianSort(array, f, asc) { for (var i = array.length; i;) { var o = array[--i]; array[i] = [].concat(f.call(o, o, i), o); } array.sort(function(a, b) { for (var i = 0, len = a.length; i < len; ++i) { if (a[i] != b[i]) return a[i] < b[i] ? asc ? -1 : 1 : 1; } return 0; }); for (var i = array.length; i;) { array[--i] = array[i][array[i].length - 1]; } return array; } arr = [] arr.push({ date: new Date(1494434112806) }) arr.push({ date: new Date(1494434118181) }) arr.push({ date: new Date(1494434127341) }) console.log(JSON.stringify(arr)); arr = schwartzianSort(arr, function(o) { return o.date }, false) console.log("DESC", JSON.stringify(arr)); arr = schwartzianSort(arr, function(o) { return o.date }, true) console.log("ASC", JSON.stringify(arr));
简单的一行解决方案为我排序日期:
sort((a, b) => (a < b ? 1 : -1))
谢谢上面那些精彩的回答。我想了一个有点复杂的答案。只是给那些想比较不同答案的人。
const data = [
'2-2018', '1-2018',
'3-2018', '4-2018',
'1-2019', '2-2019',
'3-2019', '4-2019',
'1-2020', '3-2020',
'4-2020', '1-2021'
]
let eachYearUniqueMonth = data.reduce((acc, elem) => {
const uniqueDate = Number(elem.match(/(\d+)\-(\d+)/)[1])
const uniqueYear = Number(elem.match(/(\d+)\-(\d+)/)[2])
if (acc[uniqueYear] === undefined) {
acc[uniqueYear] = []
} else{
if (acc[uniqueYear] && !acc[uniqueYear].includes(uniqueDate)) {
acc[uniqueYear].push(uniqueDate)
}
}
return acc;
}, {})
let group = Object.keys(eachYearUniqueMonth).reduce((acc,uniqueYear)=>{
eachYearUniqueMonth[uniqueYear].forEach(uniqueMonth=>{
acc.push(`${uniqueYear}-${uniqueMonth}`)
})
return acc;
},[])
console.log(group); //["2018-1", "2018-3", "2018-4", "2019-2", "2019-3", "2019-4", "2020-3", "2020-4"]