1

I have an array as follow (date in mm-dd-yyyy format) in javascript.

[ 
  {'12-11-2018': 'NA' },
  { '12-05-2018': 'NA' },
  { '12-09-2018': 'pass' },
  { '12-07-2018': 'pass' },
  { '12-10-2018': 'pass' },
  { '12-08-2018': 'pass' },
  { '12-06-2018': 'pass' } 
]

I want to sort it using the date in ascending order.

Expected output

[ 
  { '12-05-2018': 'NA' },
  { '12-06-2018': 'pass' },
  { '12-07-2018': 'pass' },
  { '12-08-2018': 'pass' },
  { '12-09-2018': 'pass' },
  { '12-10-2018': 'pass' },
  { '12-11-2018': 'NA' } 
]
Mohammad
  • 21,175
  • 15
  • 55
  • 84
Shanthi
  • 686
  • 3
  • 11
  • 22

3 Answers3

2

You can get the first key of each object, convert it to a date, then run a sort by comparing these dates:

const data = [{
    '12-11-2018': 'NA'
  },
  {
    '12-05-2018': 'NA'
  },
  {
    '12-09-2018': 'pass'
  },
  {
    '12-07-2018': 'pass'
  },
  {
    '12-10-2018': 'pass'
  },
  {
    '12-08-2018': 'pass'
  },
  {
    '12-06-2018': 'pass'
  }
]

const getDate = str => {
  const parts = str.split('-')
  return new Date(parts[0], parts[1] - 1, parts[2])
}

const getFirstKey = obj => Object.keys(obj)[0]

const fullConversion = dateStr => getDate(getFirstKey(dateStr))

const sorter = (a, b) => fullConversion(a) < fullConversion(b) ? -1 : 1

const result = data.sort(sorter)

console.dir(result)
OliverRadini
  • 6,238
  • 1
  • 21
  • 46
2

Assuming that the dates are in MM-DD-YYYY format, you can sort them by reformatting as ISO 8601 and using localeCompare to sort as strings. That avoids the built-in Date parser and associated issues, e.g.

var data = [ 
  {'12-11-2018': 'NA' },
  { '12-05-2018': 'NA' },
  { '12-09-2018': 'pass' },
  { '12-07-2018': 'pass' },
  { '12-10-2018': 'pass' },
  { '12-08-2018': 'pass' },
  { '12-06-2018': 'pass' } 
];

let mix = d => d.replace(/(\d+)-(\d+)-(\d+)/,'$3$1$2');
let key = Object.keys;

data.sort((a, b) => mix(key(a)[0]).localeCompare(mix(key(b)[0])));
    
console.log(JSON.stringify(data).replace(/,/g,',\n '));
RobG
  • 142,382
  • 31
  • 172
  • 209
0

You need to use Array.sort() to sorting array. So use it and in function get key of object (date) using Object.keys() and convert it to date object and then compare it.

var res = arr.sort((a,b) => new Date(Object.keys(a)[0]) - new Date(Object.keys(b)[0]));

var arr = [
  {'12-11-2018': 'NA' },
  { '12-05-2018': 'NA' },
  { '12-09-2018': 'pass' },
  { '12-07-2018': 'pass' },
  { '12-10-2018': 'pass' },
  { '12-08-2018': 'pass' },
  { '12-06-2018': 'pass' } 
];
var res = arr.sort((a,b) => new Date(Object.keys(a)[0]) - new Date(Object.keys(b)[0]));
console.log(res);

Update:

The dates format isn't javascript valid date format, so maybe the code doesn't work in some browser. You can convert dates to javascript valid format to preventing this behavior.

let validDate = obj => {
  let arr = Object.keys(obj)[0].split('-');
  return new Date([arr[2], arr[0], arr[1]].join('-'));
};
let res = arr.sort((a,b) => validDate(a) - validDate(b));

let arr = [
  {'12-11-2018': 'NA' },
  { '12-05-2018': 'NA' },
  { '12-09-2018': 'pass' },
  { '12-07-2018': 'pass' },
  { '12-10-2018': 'pass' },
  { '12-08-2018': 'pass' },
  { '12-06-2018': 'pass' } 
];
let validDate = obj => {
  let arr = Object.keys(obj)[0].split('-');
  return new Date([arr[2], arr[0], arr[1]].join('-'));
};
let res = arr.sort((a,b) => validDate(a) - validDate(b));

console.log(res);
Mohammad
  • 21,175
  • 15
  • 55
  • 84
  • 2
    '12-08-2018' is not a format supported by ECMA-262, so parsing is implementation dependent and you can't be sure that the objects will be sorted as expected using the built-in parser. – RobG Dec 11 '18 at 13:03
  • 1
    Reversing the order doesn't help. The OP is in m-d-y order, reversing gives y-d-m which is less likely to be parsed as required. – RobG Dec 11 '18 at 19:33