Sort by Date (Newest)

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.sort(function(o1,o2){
return sort_o1_before_o2 ? -1 : sort_o1_after_o2 ? 1 : 0;
});

Generic, Powerful Answer

Define a custom non-enumerable sortBy function using a Schwartzian transform on all arrays :

(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;
}
})();

Use it like so:

array.sortBy(function(o){ return o.date });

If your date is not directly comparable, make a comparable date out of it, e.g.

array.sortBy(function(o){ return new Date( o.date ) });

You can also use this to sort by multiple criteria if you return an array of values:

// Sort by date, then score (reversed), then name
array.sortBy(function(o){ return [ o.date, -o.score, o.name ] };

See http://phrogz.net/JS/Array.prototype.sortBy.js for more details.

Javascript sort object array by recent date to oldest date?

You could do something like the following.

const medList = [{
"name": "Insulin",
"data": [{
"dose": 20,
"date": "12/21/2019"
},
{
"dose": 10,
"date": "2/15/2020"
}, {
"dose": 20,
"date": "1/18/2020"
}
]
},
{
"name": "Viagra",
"data": [{
"dose": 20,
"date": "9/9/2019"
},
{
"dose": 10,
"date": "12/15/2020"
}, {
"dose": 20,
"date": "1/22/2020"
}
]
},
]

const sortedViagra = medList[1].data.sort((a, b) => new Date(b.date) - new Date(a.date));

const sortedMeds = medList.map(med => {
return med.data.sort((a, b) => new Date(b.date) - new Date(a.date));
});

console.log('sortedMeds --->', sortedMeds)
console.log('sortedViagra -->', sortedViagra)

How to sort the date form to latest to the oldest in array of objects react js

As you can see in your console screenshot, those aren't actually Date objects, but strings.

So you have to map the strings to Date objects and compare them afterward.

const sortedDates = array?
.map(obj => { return { ...obj, date: new Date(obj.date) } })
.sort((a, b) => b.date - a.date)

Order by descending date - month, day and year

I'm guessing EventDate is a char or varchar and not a date otherwise your order by clause would be fine.

You can use CONVERT to change the values to a date and sort by that

SELECT * 
FROM
vw_view
ORDER BY
CONVERT(DateTime, EventDate,101) DESC

The problem with that is, as Sparky points out in the comments, if EventDate has a value that can't be converted to a date the query won't execute.

This means you should either exclude the bad rows or let the bad rows go to the bottom of the results

To exclude the bad rows just add WHERE IsDate(EventDate) = 1

To let let the bad dates go to the bottom you need to use CASE

e.g.

ORDER BY 
CASE
WHEN IsDate(EventDate) = 1 THEN CONVERT(DateTime, EventDate,101)
ELSE null
END DESC

Sort array of objects with date field by date

You don't really need lodash. You can use JavaScript's Array.prototype.sort method.

You'll need to create Date objects from your date strings before you can compare them.

var myArray = [{  name: "Joe Blow",  date: "Mon Oct 31 2016 00:00:00 GMT-0700 (PDT)"}, {  name: "Sam Snead",  date: "Sun Oct 30 2016 00:00:00 GMT-0700 (PDT)"}, {  name: "John Smith",  date: "Sat Oct 29 2016 00:00:00 GMT-0700 (PDT)"}];
myArray.sort(function compare(a, b) { var dateA = new Date(a.date); var dateB = new Date(b.date); return dateA - dateB;});
console.log(myArray);

How can I sort a date and time from oldest to newest in the format of ['2020-05-27 15:26:57', '2020-05-22 16:40:58']?

calling sorted() on your list should do the trick:

lst = ['2020-05-27 15:26:57','2020-06-27 15:26:57','2020-03-27 15:26:57']
sorted(lst)

Output: ['2020-03-27 15:26:57', '2020-05-27 15:26:57', '2020-06-27 15:26:57']

We can also use lst.sort() to do the trick in the following way:

lst.sort()
lst
Output: ['2020-03-27 15:26:57', '2020-05-27 15:26:57', '2020-06-27 15:26:57']

Sort Array by Date (Newest first) in Angular

Override sort function, parse Date and compare it

this.newsList.sort((a,b)=>{
const dt1 = Date.parse(a.author.date);
const dt2 = Date.parse(b.author.date);

if (dt1 < dt2) return 1;
if (dt1 > dt2) return -1;
return 0;
});

How to sort an array of objects by date?

As has been pointed out in the comments, the definition of recent isn't correct javascript.

But assuming the dates are strings:

var recent = [
{id: 123,age :12,start: "10/17/13 13:07"},
{id: 13,age :62,start: "07/30/13 16:30"}
];

then sort like this:

recent.sort(function(a,b) { 
return new Date(a.start).getTime() - new Date(b.start).getTime()
});

More details on sort function from W3Schools

How can I show the latest data by date using sort in react typescript

This is a typescript error, the sort comparator function accepts only numbers.

Note: Without getTime it works in javascript.

const data = [{
date: "Mon Mar 07 2022",
},
{
date: "Mon Mar 01 2022",
},
{
date: "Mon Mar 06 2022",
},
];
const latestData = data.sort(
(a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()
);
console.log(latestData);


Related Topics



Leave a reply



Submit