aTo === bTo will never be equal as you define it. You are comparing links to objects that are always different. You will need to use the .isSame method. You can verify this by trying this:
const a = moment().startOf('day'); const b = moment().startOf('day'); console.log(a === b);
Here is what I would do:
// Helper function to create moments function parseMoment(date) { return moment(date === null ? undefined : date); } const sortedWorkHistory = workHistory .sort((a, b) => parseMoment(a.get('to')).diff(parseMoment(b.get('to')))) .sort((a, b) => { if(parseMoment(a.get('to')).isSame(parseMoment(b.get('to')))) { return parseMoment(a.get('from')).diff(parseMoment(b.get('from'))) } return 0; });
source share