[英]Javascript - Sort months array in chronological order basis current month
I have a list of months in javascript.我在 javascript 中有一个月份列表。 I want to sort it in chronological order based on the current date.
我想根据当前日期按时间顺序对其进行排序。
Example monthsArr = ["March", "February", "November", "December", "January"]
例如
monthsArr = ["March", "February", "November", "December", "January"]
if current month is November, sorted array should be ["Nov", "Dec", "Jan", "Feb", "Mar"]
如果当前月份是 11 月,则排序数组应为
["Nov", "Dec", "Jan", "Feb", "Mar"]
if current month is Jan, sorted array should be ["Jan", "Feb", "Mar", "Nov", "Dec"]
如果当前月份是 Jan,排序数组应该是
["Jan", "Feb", "Mar", "Nov", "Dec"]
Don't worry about conversions to enums or key value pairs.不要担心转换为枚举或键值对。 It can be handled.
可以处理。 What I am looking for is the logic on how to implement this sort function.
我正在寻找的是如何实现这种排序功能的逻辑。
like this:像这样:
var months = { January: 0, February: 1, March: 2, April: 3, May: 4, // et&c November: 10, December: 11 }; var monthsArr = ["March", "February", "November", "December", "January"]; var month = new Date().getMonth(); monthsArr.sort(function(m1, m2) { var n1 = months[m1], n2 = months[m2]; if (n1 < month) { n1 = n1 + 12; } if (n2 < month) { n2 = n2 + 12; } return n1 - n2; }); console.log(monthsArr);
have a mapping from name and month number like有一个名字和月份的映射,比如
var months =
{
January: 0,
February: 1,
March: 2,
April: 3,
May: 4,
// et&c
November: 10,
December: 11
};
get the current month获取当前月份
var month = new Date().getMonth();
sort the array adding 12 to months that are earlier than month
.对数组进行排序,将 12 添加到比
month
早的month
。 that puts them after months that are the same as this month or later.这将它们置于与本月或之后相同的月份之后。
monthsArr.sort(function(m1, m2)
{
var n1 = months[m1], n2 = months[m2];
if (n1 < month)
{
n1 = n1 + 12;
}
if (n2 < month)
{
n2 = n2 + 12;
}
return n1 - n2;
});
I would suggest you to get the index of current month,我建议你获取当月的索引,
and then use array slice to recompose the array starting from that index:然后使用数组切片从该索引开始重组数组:
var m = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] var cur = new Date().getMonth() var sorted = [...m.slice(cur), ...m.slice(0,cur)] console.log(sorted)
You could take an object for the month values and take a wanted month to be the first and sort by a check if the actual month is smaller than the first month or by the month value.您可以为月份值取一个对象,并将想要的月份作为第一个,并通过检查实际月份是否小于第一个月或按月份值进行排序。
function sort(array, first) { var months = { January: 1, February: 2, March: 3, April: 4, May: 5, June: 6, July: 7, August: 8, September: 9, October: 10, November: 11, December: 12 }; return array.sort((a, b) => (months[a] < months[first]) - (months[b] < months[first]) || months[a] - months[b] ); } console.log(...sort( ["March", "February", "November", "December", "January"], "November")); console.log(...sort( ["March", "February", "November", "December", "January"], "January"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.