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

js sort by date

array.sort((a,b) =>  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

sort by date javascript

array.sort((a,b)=>a.getTime()-b.getTime());
Comment

sort array by date in javascript

//This method will be sort array in ascending order:
var numArray = [5, 10, 12, 9, 31, 21, 18, 55, 39, 40];
numArray.sort((a, b) => {
  return a - b;
});
console.log(numArray);

//This method will be sort array in descending order:
numArray.sort((a, b) => {
  return b - a;
});

console.log(numArray)

//sort array by date

// Name Z to A
array.sort((a, b) => (a.name > b.name ? -1 : 1))

console.log(array)
// Sort by date
array.sort((a,b) =>  new Date(a.date) - new Date(b.date));

console.log(array)
Comment

sort by date javascript

array.sort(function(a,b){return a.getTime() - b.getTime()});
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to get random boolean in javascript 
Javascript :: jquery get image src 
Javascript :: text align-center js 
Javascript :: mongo delete a key in matching documents 
Javascript :: random code js 
Javascript :: Copy document DOM without reference 
Javascript :: open json file r 
Javascript :: sequelize order includes 
Javascript :: valid email patter check jquery 
Javascript :: javascript sleep thread 
Javascript :: javascript select random element from array 
Javascript :: replace all words in string jquery 
Javascript :: regex for time in hh:mm:ss 
Javascript :: react router dom 
Javascript :: running a sails js app 
Javascript :: Unhandled rejection TypeError: Article.findById is not a function sequelize 
Javascript :: python dictionary to json 
Javascript :: remove unused dependencies in React Ask Question 
Javascript :: checkbox click event jquery 
Javascript :: data binding on checkbox angular 
Javascript :: htmlWebpackPlugin.options.title 
Javascript :: node list files in directory 
Javascript :: Check if a JavaScript string is a URL 
Javascript :: has decimal javascript 
Javascript :: equivalent of useHistory in react 
Javascript :: react image wont show 
Javascript :: js foreach determine if last 
Javascript :: loopback date greater than 
Javascript :: login to discord using token 
Javascript :: import paper material ui 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =