[英]Array.sort() compare function returns undefined
我正在尝试从最早到最新的字符串日期排序。 我设置了几个比较功能,但控制台说a
是undefined
。 怎么了?
//Sort an array of dates in this format
const dates = [
'10',
'23 Apr 2018',
'01 Jun 1943',
'05 Aug 2055',
'22 Sep 1902'
'18 Aug 1970',
'01 Jan 1940',
'08 Mar 2018',
'11 Feb 1982',
'17 Mar 1927',
];
//remove the data that is not in the correct format
const cleanedDates = dates.filter(date => date.length === 11);
//isolate the day, convert to number
const getDay = (str) => {
return parseInt(str.slice(0,2));
};
//create a dictionary of months
const monthDict = {
Jan: 1,
Feb: 2,
Mar: 3,
Apr: 4,
May: 5,
Jun: 6,
Jul: 7,
Aug: 8,
Sep: 9,
Oct: 10,
Nov: 11,
Dec: 12
};
//get the month value via dictionary
const getMonth = (str) => {
const month = str.slice(3,6);
return monthDict[month];
};
//get the year, convert to number
const getYear = (str) => {
return parseInt(str.slice(7));
}
//comparison helper functions
//compare day
const compareDay = (a,b) => {
if (getDay(a) < getDay(b)) {
return -1;
} else if (getDay(a) === getDay(b)) {
return 0;
}
} else if (getDay(a) > getDay(b)) {
return 1;
}
};
//compare month
const compareMonth = (a,b) => {
if (getMonth(a) < getMonth(b)) {
return -1
} else if (getMonth(a) === getMonth(b)) {
compareDay(a,b);
} else if (getMonth(a) > getMonth(b)) {
return 1;
}
};
//compare year
const compareYear = (a,b) => {
if (getYear(a) < getYear(b)) {
return -1;
} else if (getYear(a) === getYear(b)) {
compareMonth(a,b);
}
} else if (getYear(a) > getYear(b)) {
return 1
}
};
//sort array
const sortedArray = cleanedDates.sort((a,b) => compareYear(a,b));
console.log(sortedArray);
您的语法不正确。 其余的为我工作:)。 当您对22 Sep 1902
的值进行const date
时,
会丢失a。 如果执行else if,则在两个位置上有额外的}
。
解决该问题的方法:
//Sort an array of dates in this format const dates = [ '10', '23 Apr 2018', '01 Jun 1943', '05 Aug 2055', '22 Sep 1902', '18 Aug 1970', '01 Jan 1940', '08 Mar 2018', '11 Feb 1982', '17 Mar 1927' ]; //remove the data that is not in the correct format const cleanedDates = dates.filter(date => date.length === 11); //isolate the day, convert to number const getDay = (str) => { return parseInt(str.slice(0, 2)); }; //create a dictionary of months const monthDict = { Jan: 1, Feb: 2, Mar: 3, Apr: 4, May: 5, Jun: 6, Jul: 7, Aug: 8, Sep: 9, Oct: 10, Nov: 11, Dec: 12 }; //get the month value via dictionary const getMonth = (str) => { const month = str.slice(3, 6); return monthDict[month]; }; //get the year, convert to number const getYear = (str) => { return parseInt(str.slice(7)); } //comparison helper functions //compare day const compareDay = (a, b) => { if (getDay(a) < getDay(b)) { return -1; } else if (getDay(a) === getDay(b)) { return 0; } else if (getDay(a) > getDay(b)) { return 1; } }; //compare month const compareMonth = (a, b) => { if (getMonth(a) < getMonth(b)) { return -1 } else if (getMonth(a) === getMonth(b)) { compareDay(a, b); } else if (getMonth(a) > getMonth(b)) { return 1; } }; //compare year const compareYear = (a, b) => { if (getYear(a) < getYear(b)) { return -1; } else if (getYear(a) === getYear(b)) { compareMonth(a, b); } else if (getYear(a) > getYear(b)) { return 1 } }; //sort array const sortedArray = cleanedDates.sort((a, b) => compareYear(a, b)); console.log(sortedArray);
清理dates数组后,没有语法错误,请尝试以下操作:
// convert to date
dates.map( el => new Date(el));
// sort it
dates.sort( (a,b) => a>b);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.