用即将到来的日期将日期与字符串排序为数组

时间:2019-01-23 16:23:08

标签: javascript arrays vue.js ecmascript-6

因此,我试图对数组进行排序,以使第一项与当前的日期和月份或最接近的条目相同。

我的数组如下:

[
    [
       "Firstname Lastname",
        "1979-01-03",
        "40"
    ],
    [
        "Firstname Lastname",
        "1996-01-23",
        "23"
    ],
    [
        "Firstname Lastname",
        "1977-01-28",
        "41"
    ],
    [
        "Firstname Lastname",
        "1983-03-11",
        "35"
    ],
    [
       "Firstname Lastname",
       "1977-03-30",
        "41"
    ],
    [
       "Firstname Lastname",
        "1975-05-08",
        "43"
    ]
]

我确实弄清楚了如何根据月份中的日期对数组进行排序,但是随后它忽略了月份本身

relativeYearDay(date) {
    let differenceDay = new Date(date).getDate() - new Date().getDate();

     if (differenceDay < 0) {
         differenceDay += 365;
     }

     return differenceDay;
}

getUpcomingBirthdays() {
     return this.birthdays.slice(0).sort((a, b) => {
         return this.relativeYearDay(a[1]) - this.relativeYearDay(b[1]);
    });
},

就像我提到的那样,它会根据每月的某天返回一个排序的数组。

在一天和一个月中,我该怎么做?

4 个答案:

答案 0 :(得分:1)

您的原始答案非常接近。排序时,您只需要弄清楚用户下一个即将到来的生日的日期。

const birthdays = [
    [ "Firstname Lastname", "1979-01-03", "40" ],
    [ "Firstname Lastname", "1996-01-23", "23" ],
    [ "Firstname Lastname", "1977-01-28", "41" ],
    [ "Firstname Lastname", "1983-03-11", "35" ],
    [ "Firstname Lastname", "1977-03-30", "41" ],
    [ "Firstname Lastname", "1975-05-08", "43" ]
];


function getNextBirthday(date) {
    // Current Date
    let currentDate = new Date();

    // Set the users birthday to this year (originally from thier birth year)
    let birthday = new Date(date);
    birthday.setFullYear(currentDate.getFullYear());

    // If the birthday has already occured this year.  Then thier next birthday is next year.
    if (birthday - currentDate < 0) {
        birthday.setFullYear(currentDate.getFullYear() + 1);
    }

    // Return the users next birthday as a date.
    return birthday;
}

function getUpcomingBirthdays() {
     return birthdays.slice(0).sort((a, b) => {
         return getNextBirthday(a[1]) - getNextBirthday(b[1]);
    });
}

编辑:添加了注释并修复了代码中的小错误。

答案 1 :(得分:1)

实际上,由于您要按下一个生日进行排序,因此与dates比较时,可以将当前年份设置为所有current date。如果它们之间的差为负(即已经发生生日),则可以从现在起增加1年的偏移量。

const birthdays = [
    ["Firstname Lastname", "1979-01-03", "40"],
    ["Firstname Lastname", "1996-01-23", "23"],
    ["Firstname Lastname", "1977-01-28", "41"],
    ["Firstname Lastname", "1983-03-11", "35"],
    ["Firstname Lastname", "1977-03-30", "41"],
    ["Firstname Lastname", "1975-05-08", "43"]
];

function distanceToBirthday(date)
{
    let currDate = new Date();
    currDate.setHours(0, 0, 0, 0);
    let currYear = currDate.getFullYear();

    let offset = new Date();
    offset.setHours(0, 0, 0, 0);
    offset.setFullYear(currYear + 1);

    date = new Date(date + " 00:00");
    date.setFullYear(currYear);

    let diff = date - currDate;
    return (diff < 0) ? diff + offset.getTime() : diff;
}

function getUpcomingBirthdays(bdays)
{
    return bdays.slice(0).sort(
        (a, b) => distanceToBirthday(a[1]) - distanceToBirthday(b[1])
    );
}

console.log(getUpcomingBirthdays(birthdays));

答案 2 :(得分:0)

我误解了问题,以为您想要的是距当前日期最近的日期。

使用简单的编码...可能会给您一些思路,将其变成排序例程

const data = [
    [
       "Firstname Lastname",
        "1979-01-03",
        "40"
    ],
    [
        "Firstname Lastname",
        "1996-01-23",
        "23"
    ],
    [
        "Firstname Lastname",
        "1977-01-28",
        "41"
    ],
    [
        "Firstname Lastname",
        "1983-03-11",
        "35"
    ],
    [
       "Firstname Lastname",
       "1977-03-30",
        "41"
    ],
    [
       "Firstname Lastname",
        "1975-05-08",
        "43"
    ]
];
const now = new Date().getTime();
let nearestIndex = -1;
let nearest = 0;

data.forEach( (item, index) => {
  if(nearest ==0 || now-new Date(item[1]).getTime() < nearest) {
    nearest = now-new Date(item[1]).getTime();
    nearestIndex = index;
  }  
});

console.log(`Nearest date is at index ${nearestIndex}`, data[nearestIndex], nearest, nearestIndex);

答案 3 :(得分:0)

您可以使用带有部分的排序算法对数组进行排序,其中您将日期作为分隔符,并将较小的值移到数组的末尾,然后按日期对所有其他值进行排序。

此方法使用部分字符串,因为ISO 8601日期可以按字符串排序。

var array = [["Firstname Lastname", "1979-01-03", "40"], ["Firstname Lastname", "1996-01-23", "23"], ["Firstname Lastname", "1977-01-28", "41"], ["Firstname Lastname", "1983-03-11", "35"], [ "Firstname Lastname","1977-03-30", "41"], [ "Firstname Lastname", "1975-05-08", "43"]],
    day = (new Date).toISOString().slice(5, 10)

array.sort(({ 1: a }, { 1: b }) =>
    (a.slice(5) < day) - (b.slice(5) < day) || a.slice(5).localeCompare(b.slice(5)));

console.log(array.map(a => a.join(' | ')));