下劃線的_sortBy
工程工作演示。你需要檢查和比較不同的情況,如果你有'week'
出現在標題或沒有(或檢查開始時間和結束時間,由你):
interface IDateRange {
title: string;
startTime: Date;
endTime: Date;
}
const dateRanges: IDateRange[] = [
{
"title": "February 2016",
"startTime": "2016-02-01T10:00:00.000Z",
"endTime": "2016-03-01T09:59:59.000Z"
},
{
"title": "Week of February 28 2016",
"startTime": "2016-02-28T05:00:00.000Z",
"endTime": "2016-03-06T04:59:59.000Z"
},
{
"title": "March 2016",
"startTime": "2016-03-01T10:00:00.000Z",
"endTime": "2016-04-01T07:59:59.000Z"
},
{
"title": "Week of March 06 2016",
"startTime": "2016-03-06T05:00:00.000Z",
"endTime": "2016-03-13T04:59:59.000Z"
},
{
"title": "Week of March 13 2016",
"startTime": "2016-03-13T05:00:00.000Z",
"endTime": "2016-03-20T03:59:59.000Z"
},
{
"title": "October 2016",
"startTime": "2016-10-01T08:00:00.000Z",
"endTime": "2016-11-01T07:59:59.000Z"
},
{
"title": "Week of October 02 2016",
"startTime": "2016-10-02T04:00:00.000Z",
"endTime": "2016-10-09T03:59:59.000Z"
},
{
"title": "Week of October 30 2016",
"startTime": "2016-10-30T04:00:00.000Z",
"endTime": "2016-11-06T03:59:59.000Z"
},
{
"title": "November 2016",
"startTime": "2016-11-01T08:00:00.000Z",
"endTime": "2016-12-01T09:59:59.000Z"
},
{
"title": "Week of November 13 2016",
"startTime": "2016-11-13T05:00:00.000Z",
"endTime": "2016-11-20T04:59:59.000Z"
},
{
"title": "Week of November 20 2016",
"startTime": "2016-11-20T05:00:00.000Z",
"endTime": "2016-11-27T04:59:59.000Z"
},
{
"title": "Week of November 27 2016",
"startTime": "2016-11-27T05:00:00.000Z",
"endTime": "2016-12-04T04:59:59.000Z"
},
{
"title": "December 2016",
"startTime": "2016-12-01T10:00:00.000Z",
"endTime": "2017-01-01T09:59:59.000Z"
},
{
"title": "Week of December 04 2016",
"startTime": "2016-12-04T05:00:00.000Z",
"endTime": "2016-12-11T04:59:59.000Z"
},
{
"title": "Week of December 11 2016",
"startTime": "2016-12-11T05:00:00.000Z",
"endTime": "2016-12-18T04:59:59.000Z"
},
{
"title": "Week of December 25 2016",
"startTime": "2016-12-25T05:00:00.000Z",
"endTime": "2017-01-01T04:59:59.000Z"
},
{
"title": "January 2017",
"startTime": "2017-01-01T10:00:00.000Z",
"endTime": "2017-02-01T09:59:59.000Z"
},
{
"title": "Week of January 15 2017",
"startTime": "2017-01-15T05:00:00.000Z",
"endTime": "2017-01-22T04:59:59.000Z"
},
{
"title": "Week of January 29 2017",
"startTime": "2017-01-29T05:00:00.000Z",
"endTime": "2017-02-05T04:59:59.000Z"
},
{
"title": "February 2017",
"startTime": "2017-02-01T10:00:00.000Z",
"endTime": "2017-03-01T09:59:59.000Z"
},
{
"title": "Week of February 05 2017",
"startTime": "2017-02-05T05:00:00.000Z",
"endTime": "2017-02-12T04:59:59.000Z"
},
{
"title": "Week of February 12 2017",
"startTime": "2017-02-12T05:00:00.000Z",
"endTime": "2017-02-19T04:59:59.000Z"
},
{
"title": "March 2017",
"startTime": "2017-03-01T10:00:00.000Z",
"endTime": "2017-04-01T07:59:59.000Z"
},
{
"title": "Week of March 05 2017",
"startTime": "2017-03-05T05:00:00.000Z",
"endTime": "2017-03-12T04:59:59.000Z"
},
{
"title": "Week of March 12 2017",
"startTime": "2017-03-12T05:00:00.000Z",
"endTime": "2017-03-19T03:59:59.000Z"
},
{
"title": "Week of March 19 2017",
"startTime": "2017-03-19T04:00:00.000Z",
"endTime": "2017-03-26T03:59:59.000Z"
},
{
"title": "Week of March 26 2017",
"startTime": "2017-03-26T04:00:00.000Z",
"endTime": "2017-04-02T03:59:59.000Z"
}
];
////==============================
console.clear();
//Convert to real data objects...
for (const range of dateRanges) {
range.startTime = moment(range.startTime).utc().toDate();
range.endTime = moment(range.endTime).utc().toDate();
}
//OLD
const librarySort = _.sortBy(dateRanges, (range: IDateRange) => {
if (moment(range.endTime).diff(range.startTime, "days") > 7) {
return range.endTime;
} else {
return range.startTime;
}
});
//NEW - changes made here
const nativeSort = dateRanges.sort((r1: IDateRange, r2: IDateRange) => {
let firstPartToCompare;
let secondPartToCompare;
if(r1.title.indexOf('eek') > -1) {
firstPartToCompare = r1.startTime;
} else {
firstPartToCompare = r1.endTime;
}
if(r2.title.indexOf('eek') > -1) {
secondPartToCompare = r2.startTime;
} else {
secondPartToCompare = r2.endTime;
}
if (firstPartToCompare > secondPartToCompare) {
return 1;
} else if (firstPartToCompare < secondPartToCompare) {
return -1;
}
return 0;
});
console.info("CORRECT order - old way")
console.table(librarySort);
console.info("ATTEMPT to duplicate - new way")
console.table(nativeSort);
http://codepen.io/anon/pen/GWVrwB
使用帶'字符串比較>'這將是詞典是日期不好的主意。使用'.valueOf()'獲取數值紀元時間值並按此進行排序。請務必首先轉換爲UTC。 –
非常感謝,這就是訣竅。我知道我在做一些簡單的事情。以下是我爲改變日期而不是使用單詞「week」的基礎: 'const r1IsWholeMonth = r1.endTime.getTime() - r1.startTime.getTime()> 604800000 ; const firstPartToCompare = r1IsWholeMonth? r1.endTime:r1.startTime;' –
@ChrisBarr沒有問題,對我來說已經很晚了,所以我想保持簡單。比較時差也是很好的方法,同時考慮到Aluan的評論。 – Oskar