gpt4 book ai didi

javascript - Array.sort() 比较函数返回未定义

转载 作者:行者123 更新时间:2023-12-01 00:59:57 25 4
gpt4 key购买 nike

我正在尝试将字符串日期数组从最旧到最新进行排序。我设置了几个比较函数,但控制台显示 a未定义。出了什么问题?

//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 时,您缺少 ,。当您执行 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);

关于javascript - Array.sort() 比较函数返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56176566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com