JavaScript sort () method

The sort() method is used to sort the elements of an array by character encoding, sorting the array on top of the original array without making a copy.

The comparison function should take two arguments, a and b, and return the following values:

  • If a is less than b, which should precede B in the sorted array, returns a value less than 0.
  • If a is equal to b, 0 is returned.
  • If a is greater than b, return a value greater than 0.
var data = [ { name:'1', time:'2019-04-26 10:53:19' }, { name:'2', time:'2019-04-26 10:51:19' },{ name:'3', time:'2019-04-26 11:04:32' },{ name:'4', time:'2019-04-26 11:05:32' } ] data.sort(function(a,b){ return a.time < b.time ? [0: {name: "4", time: "2019-04-26 11:05:32"} 1: {name: "3", time: "2019-04-26 11:04:32"} 2: {name: "1", time: "2019-04-26 10:53:19"} 3: {name: "2", time: "2019-04-26 10:51:19"} length: 4 __proto__: Array(0) ]Copy the code