15

sortBy() in Lodash isn't sorting to the descending order, when I pass 'desc', when calling the function as const sortedData = _.sortBy(data, ['rawAvgPrice'], ['desc']);. This works fine with the ascending order. But not with the descending order. I have posted the sorting functionality which I have written. I read the thread "lodash multi-column sortBy descending" but it didn't help me with my problem. Therefore decided to post this.

    /**
     * @param {String} element - sorting object element name.
     * @param {String} dir - the direction of the sort ASC/DESC.
     * @param {Boolean} flag - Signaling to sort the table and update.
     */
    sortTableNew(element, dir, flag) {
      let direction = dir;
      
      // Change the sorting from ASC to DESC/ DESC to ASC
      if (flag) {
        direction = dir === 'asc' ? 'desc' : 'asc';
      }
      
      // Getting the current open tabs details
      const { activeTab } = this.$props.state.keywordSearch;
      const { data } = this.$props.state.keywordSearch.tabs[activeTab];

      const sortedData = _.sortBy(data, [element], [direction]);
      
      // Updating the cache to dispatch data to the table
      cachedHelper.updateCacheKeyword(cachedHelper.SCREEN_TYPE.keywordSearch, activeTab, sortedData);
      cachedHelper.updateCacheSortType(cachedHelper.SCREEN_TYPE.keywordSearch, activeTab, direction, column);
    },
Nine3KiD
  • 473
  • 2
  • 4
  • 20
  • As alternative quick solution after sort ascending, just do array.reverse() – Steve Tomlin Jun 03 '21 at 14:15
  • @SteveTomlin even I saw this life hack. But I was looking for a legit/proper solution. this works as well. Plus when I do that it will move the array where that element does not exist. – Nine3KiD Jun 03 '21 at 14:25

3 Answers3

32

From the lodash documentation we find this when searching for _.sortBy: "Creates an array of elements, sorted in ascending order by the results of running each element in a collection thru each iteratee. "

From that we can see that _.sortBy will always return an sorted array in ascending order.

What you can try is to use _.orderBy instead like this: _.orderBy(users, 'age', 'desc');

Munge
  • 474
  • 5
  • 7
9

You can try using the Lodash's orderBy method. Worked like a charm for me.

var users = [
  { 'user': 'fred',   'age': 48 },
  { 'user': 'barney', 'age': 34 },
  { 'user': 'fred',   'age': 40 },
  { 'user': 'barney', 'age': 36 }
];
 
// Sort by `user` in ascending order and by `age` in descending order.
_.orderBy(users, ['user', 'age'], ['asc', 'desc']);
// => objects for [['barney', 36], ['barney', 34], ['fred', 48],

You can check the official documentation here Lodash orderBy

jateen
  • 642
  • 3
  • 13
4

Another option not mentioned here is to just .reverse() the result:

const users = [{
    'user': 'alice',
    'age': 48
  },
  {
    'user': 'bob',
    'age': 34
  },
  {
    'user': 'claire',
    'age': 40
  },
  {
    'user': 'dennis',
    'age': 36
  }
];

console.log(_.sortBy(users, 'age').reverse());
<script src="https://unpkg.com/lodash@4.17.21/lodash.js"></script>
Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49
  • The caveat is that if you need a stable sort which preserves the existing order otherwise than the specified property, this won't work. – eclux Apr 22 '23 at 19:58