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

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

PREVIOUS NEXT
Code Example
Javascript :: javascript get element by custom attribute 
Javascript :: random string js 
Javascript :: vue js select option disabled false 
Javascript :: find the max length of string elements in an array 
Javascript :: how to check checked checkbox in jquery 
Javascript :: vue js cdn link 
Javascript :: javascript sort array strings alphabetically 
Javascript :: how to fetch the all input element id value 
Javascript :: finding by sub property of an object in mongo 
Javascript :: add multiple class list at once in js 
Javascript :: generate random integer javascript 
Javascript :: Shuffle a Sting in JavaScript 
Javascript :: .env file node js 
Javascript :: merge two objects javascript 
Javascript :: js get vh value 
Javascript :: js maximum number value 
Javascript :: //disable-linter-line 
Javascript :: nodejs download image from url 
Javascript :: express get host url 
Javascript :: how to get value in formgroup in angular 
Javascript :: javascript create an array of range between two numbers 
Javascript :: javascript console log execution time 
Javascript :: cannot use import statement outside a module in jest 
Javascript :: statusbar reactnati 
Javascript :: how to validate a string using regular expression in javascript 
Javascript :: how to convert milliseconds to time in javascript 
Javascript :: react native text input right 
Javascript :: import all images from folder reactjs 
Javascript :: replace all occurrences of a string in javascript 
Javascript :: data table 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =