简体   繁体   中英

How to sort array of object based on key that is date using javascript?

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' } 
]

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) 

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, eg

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM