-2

So I have some data that returns from a request that looks like this:

[{
  subject: 'something',
  email: 'someone@gmail.com',
  name: 'Bert',
  priority: '2'
},
{
  subject: 'something else',
  email: 'someoneelse@gmail.com',
  name: 'Betty',
  priority: '4'
},
{
  subject: 'another thing',
  email: 'anotherone@gmail.com',
  name: 'Joanne',
  priority: '1'
}];

But of course with a lot more items. So I'm looking to sort these items on 'priority' so the object with 4 comes first in the array. Any idea how to do this in javascript?

hxwtch
  • 135
  • 3
  • 14

4 Answers4

0

var f =[{
  subject: 'something',
  email: 'someone@gmail.com',
  name: 'Bert',
  priority: '2'
},
{
  subject: 'something else',
  email: 'someoneelse@gmail.com',
  name: 'Betty',
  priority: '4'
},
{
  subject: 'another thing',
  email: 'anotherone@gmail.com',
  name: 'Joanne',
  priority: '1'
}];

f.sort((a,b) => +b.priority - +a.priority)
console.log(f)
sarvon ks
  • 626
  • 3
  • 10
0

I wrote a generic function to do the sort by a property name as well as ascending and descending order feature.

function sortDataByPropName(data, property, order) {
    if(order === 'asc'){
      data.sort((a, b) =>  a[property] - b[property])
    } else {
      data.sort((a, b) =>  b[property] - a[property])
    }
    return data;
  }

In your case, you can simply call this function like sortDataByPropName(data, 'priority') or if you want change sort order then simply pass third argument like asc default is desc.

-1

Use sort & convert the value of priority from string to number using parseInt.

let data = [{
    subject: 'something',
    email: 'someone@gmail.com',
    name: 'Bert',
    priority: '2'
  },
  {
    subject: 'something else',
    email: 'someoneelse@gmail.com',
    name: 'Betty',
    priority: '4'
  },
  {
    subject: 'another thing',
    email: 'anotherone@gmail.com',
    name: 'Joanne',
    priority: '1'
  }
];

let sortedDt = data.sort(function(a, b) {
  return parseInt(b.priority, 10) - parseInt(a.priority, 10)
});

console.log(sortedDt)
brk
  • 48,835
  • 10
  • 56
  • 78
-1

In javascript, there is a method known as .sort() in Array which can be used for sorting. This is how it works.

  1. If compareFunction(a, b) is less than 0, sort a to an index lower than b (i.e. a comes first).
  2. If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAScript standard does not guarantee this behaviour, thus, not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.
  3. If compareFunction(a, b) is greater than 0, sort b to an index lower than a (i.e. b comes first).
  4. compareFunction(a, b) must always return the same value when given a specific pair of elements a and b as its two arguments. If inconsistent results are returned, then the sort order is undefined.

So in our case, we need to sort it in descending order. i.e objects with less priority must come at the top. For that, we need to return a number which is less than 0. Which can be done if we subtract the priority of b by priority of a.

In the code:

const items = [{
  subject: 'something',
  email: 'someone@gmail.com',
  name: 'Bert',
  priority: '2'
},
{
  subject: 'something else',
  email: 'someoneelse@gmail.com',
  name: 'Betty',
  priority: '4'
},
{
  subject: 'another thing',
  email: 'anotherone@gmail.com',
  name: 'Joanne',
  priority: '1'
}];

const sorted = items.sort((a,b) => b.priority - a.priority);

console.log(sorted);

You can learn much more about it here

Advaith
  • 2,490
  • 3
  • 21
  • 36