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

sort by date js

array.sort((a,b) =>  new Date(b.date) - new Date(a.date));
Comment

sort js array 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

typescript sort array of objects by date descending

arr = arr.sort(function (a, b) {
      var dateA = new Date(a.date_prop).getTime();
      var dateB = new Date(b.date_prop).getTime();
      return dateA < dateB ? 1 : -1; // ? -1 : 1 for ascending/increasing order
    });
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

Sort Dates JavaScript

var l = [new Date(2022, 10, 10), new Date(2022, 10, 8), new Date(2022, 10, 6), new Date(2022, 10, 15), new Date(2022, 10, 2)];
console.log(l.sort((date1, date2)=>date1-date2));
Comment

PREVIOUS NEXT
Code Example
Javascript :: Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout. 
Javascript :: how to get the number of days in a month in javascript 
Javascript :: laravel tabel json nullable 
Javascript :: show each element of an array in a different line 
Javascript :: javascript leetcode solutions 
Javascript :: npm generate package-lock.json 
Javascript :: maximum element in an array javascript 
Javascript :: blabla 
Javascript :: select value from select and pass it to useeffect 
Javascript :: javascript calculate element style 
Javascript :: can i use pipe in switch statement javascript 
Javascript :: firestorage vuetify 
Javascript :: how to make a box in p5js 
Javascript :: Pause interval button javascript 
Javascript :: add onmouseover event in javascript 
Javascript :: enum string json 
Javascript :: jquery override page title 
Javascript :: how to accept only. proper email from an input field react with functional component 
Javascript :: scale sprite matter.js 
Javascript :: grapql file upload 
Javascript :: open json file in JS in order to access data 
Javascript :: js multiple declaration 
Javascript :: angular ng build setting body min-width 
Javascript :: react native icons in one file 
Javascript :: new Intl.NumberFormat en react 
Javascript :: get current tab url in chrome extension in popup 
Javascript :: javascript uuid generator 
Javascript :: a to z in js using while 
Javascript :: jquery selected checkboxes pass as paramater to C# 
Javascript :: multiple comparison javascript 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =