How sort this array of items by date in desc order (I used loadash sortBy but it returns oldest first while I need newest first)?

NewTech Lover

How can I sort this array of objects with dates using desc order?

I tried loadsh sortBy

  if ( sort && sort == "new") {
      items = _.sortBy(items, (dateObj) => {
      return new Date(dateObj.pubDate);
      });
      }

But it returns oldest items first while I need newest first. How can be it fixed?

ISAE

Since your are already using lodash, the solution is simple and involve just a minimal modification to your current code:

items = _.orderBy(items, (dateObj) => {
      return new Date(dateObj.pubDate);
      }, 'desc');  

lodash orderBy is very similar to sortBy with the addition of a third parameter, which specify the sort order (asc or desc), and the difference that the 'iteratee' could also be 'iteratees', by specifying an array of properties to sort by, in which case the sort order could also be an array with corresponding order strings for each property.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Sort Array by Date (Newest first) in Angular

How to get only two oldest records based on 'date' from first 50 records 'ORDER BY id DESC'

How do I sort new items by newest?

How do I sort a list by newest date?

Shopify - sort blog Posts from oldest to newest - old post first

Sorting by Newest and Oldest First Not Working

How can I sort an array on the first value?

How to sort a 3 dimensional array by element containing date, from newest to oldest

How can I query my data by published date (newest first) using Apollo and Graphql?

Laravel Query Builder: How do I sort results by date - first with ascending order by future dates and then by descending order by past dates?

How can I sort this object by 'sortby' key which inside an array?

How would I print a model fields from oldest to newest?

How do I nest an array of items dividing them by their first value?

How do I swap the first and second items in an array of strings?

How do I sort an array by the first character in the string?

How do I sort by first item of array element?

How can I sort files by DESC order with Symfony Finder Component?

i need my function that skips array items to return to first item if the the array is over

How to sort a column from newest to oldest?

Sort by date on React database call not working - just returning oldest first

I need to find the first event in an array?

How do I select rows, order by date desc in Google Sheets?

Why does it only returns only the first element of the array while i've already printed all the values of the array?

How to sort cart items by date (the last modified cart item will be the first)?

How do I sort array of names first by last names and then by first name and middle name in Javascript

PHP With 2 arrays how do I get first element that exists in both arrays based on the order of the first array?

In JavaScript how do I sort an array of arrays by asc / desc

my articles shows up oldest first but i want it to show new one first like date wise new one should appear on top

ORDER BY id DESC LIMIT 100 returns first rows instead of last