Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript sort on objects date

array = [{
  			id: 1, name: "test1", date: "2020-01-05",
			id: 2, name: "test2", date: "2020-01-02"
		}]

array.sort(function (a, b) {
	var dateA = new Date(a.date), dateB = new Date(b.date)
	return dateA - dateB
});

console.log(array) //array is now sorted by date
Comment

javascript sort array of objects by date

// Price Low To High
array?.sort((a, b) => (a.price > b.price ? 1 : -1))
// Price High To Low
array?.sort((a, b) => (a.price > b.price ? -1 : 1))
// Name A to Z
array?.sort((a, b) => (a.name > b.name ? 1 : 1))
// Name Z to A
array?.sort((a, b) => (a.name > b.name ? -1 : 1))
// Sort by date
array.sort((a,b) =>  new Date(b.date) - new Date(a.date));
Comment

sort array of objects javascript by date

array.sort(function(a,b){
  // Turn your strings into dates, and then subtract them
  // to get a value that is either negative, positive, or zero.
  return new Date(b.date) - new Date(a.date);
});
Comment

How to sort an object array by date property?

// Simplest Answer
array.sort(function(a,b){
  // Turn your strings into dates, and then subtract them
  // to get a value that is either negative, positive, or zero.
  return new Date(b.date) - new Date(a.date);
});

// -------------------

// More Generic Answer
array.sort(function(o1,o2){
  if (sort_o1_before_o2)    return -1;
  else if(sort_o1_after_o2) return  1;
  else                      return  0;
});

// Or more tersely:

array.sOr more tersely:ort(function(o1,o2){
  return sort_o1_before_o2 ? -1 : sort_o1_after_o2 ? 1 : 0;
});
// -------------------

// Generic, Powerful Answer
(function(){
  if (typeof Object.defineProperty === 'function'){
    try{Object.defineProperty(Array.prototype,'sortBy',{value:sb}); }catch(e){}
  }
  if (!Array.prototype.sortBy) Array.prototype.sortBy = sb;

  function sb(f){
    for (var i=this.length;i;){
      var o = this[--i];
      this[i] = [].concat(f.call(o,o,i),o);
    }
    this.sort(function(a,b){
      for (var i=0,len=a.length;i<len;++i){
        if (a[i]!=b[i]) return a[i]<b[i]?-1:1;
      }
      return 0;
    });
    for (var i=this.length;i;){
      this[--i]=this[i][this[i].length-1];
    }
    return this;
  }
})();
Comment

PREVIOUS NEXT
Code Example
Javascript :: just number regex 
Javascript :: javascript convert float to int 
Javascript :: in array jquery 
Javascript :: how to pass laravel route name to ajax request 
Javascript :: phone number validation in yup 
Javascript :: javascript remove all whitespaces 
Javascript :: install moment in goole dev console 
Javascript :: jquery select outer html 
Javascript :: node js for loop 
Javascript :: js get element distance from top 
Javascript :: hash change listener js 
Javascript :: __WEBPACK_IMPORTED_MODULE_0_react___default.a.createContext is not a function 
Javascript :: get href attribute javascript 
Javascript :: react native rotate image 
Javascript :: add button in table using javascript 
Javascript :: innerwidth react 
Javascript :: get radio button value javascript 
Javascript :: remove all node_modules folders recursively 
Javascript :: nodejs does every worker thread need a new core 
Javascript :: _redirects in netlify 
Javascript :: settimeout node js 
Javascript :: add condition inside a className in reactjs 
Javascript :: get random number in solidity 
Javascript :: javascript get viewport dimensions 
Javascript :: JS get length of an object 
Javascript :: display pm or am on date js 
Javascript :: how to find prime numbers in an array in javascript 
Javascript :: cut text if too long javascript 
Javascript :: js push if not exists 
Javascript :: javascript fill 2d array 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =