0

I am trying to sort an array of string dates from oldest to newest. I set up a couple of compare functions, but the console is saying a is undefined. What is going wrong?

//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);
4
  • 1
    You have additional } at lines 59 and 82 which causes a javascript error. On line 7 you forgot a , (thanks to @Zigzagoon). Upon fixing those the console.log shows what you want, see : jsfiddle.net/hw4and5q Commented May 16, 2019 at 21:05
  • 2
    In your dates array you are missing a comma Commented May 16, 2019 at 21:05
  • you need to return the results of the comparing functions. Commented May 16, 2019 at 21:08
  • You have a lot of a variables, try to name them with more meaningful names, like currentDay and nextDay in compareDay so it is easier to see where the error is Commented May 16, 2019 at 21:10

2 Answers 2

2

your syntax is incorrect. The rest is working for me :). you are missing a , when you do const date on the value 22 Sep 1902. And there are extra } on two locations when you do an else if.

Fixing that will get it working:

//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);

Sign up to request clarification or add additional context in comments.

Comments

0

After cleaning the dates array and there is no syntax error, try this:

// convert to date
dates.map( el => new Date(el));
// sort it
dates.sort( (a,b) => a>b);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.