ankit lohiya
ankit lohiya

Reputation: 65

How to sort an array of objects with two keys in javascript

I have an array of objects and I want to sort it based on two keys.

var data = [{COMPONENT: 'PM-ABC', PRIORITY: '0.35'},
            {COMPONENT: 'PM', PRIORITY: '0.35'}
            {COMPONENT: 'PM', PRIORITY: ''}]

It should first sort on key COMPONENT (Ascending order) and then on PRIORITY ('' should come before number say '0.35')

I have tried below code which sorts based on only key i.e COMPONENT

data.sort(function (a, b) {
            return (a['COMPONENT'] > b['COMPONENT']) ? 1 : (a['COMPONENT'] < b['COMPONENT']) ? -1 : 0;
        });

I am expecting below result

data = [{COMPONENT: 'PM', PRIORITY: ''}
        {COMPONENT: 'PM', PRIORITY: '0.35'}
        {COMPONENT: 'PM-ABC', PRIORITY: '0.35'}]

Upvotes: 0

Views: 50

Answers (4)

Bruno Marotta
Bruno Marotta

Reputation: 488

Assuming that PRIORITY is expected to be a number and that an empty string '' is the same as 0, your solution would be:

const data = [{ COMPONENT: 'PM-ABC', PRIORITY: '0.35' }, { COMPONENT: 'PM', PRIORITY: '0.35' }, { COMPONENT: 'PM', PRIORITY: '' }];

data.sort((a, b) => 
    a.COMPONENT.localeCompare(b.COMPONENT) ||
    ((parseFloat(a.PRIORITY) || 0) - (parseFloat(b.PRIORITY) || 0))
);

console.log(data);

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386530

You could sort by stages, first COMPONENT and then PRIORITY with a check.

const
    data = [{ COMPONENT: 'PM-ABC', PRIORITY: '0.35' }, { COMPONENT: 'PM', PRIORITY: '0.35' }, { COMPONENT: 'PM', PRIORITY: '' }];

data.sort((a, b) =>
    a.COMPONENT.localeCompare(b.COMPONENT) ||
    (b.PRIORITY === '') - (a.PRIORITY === '')
);

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 1

Diego D
Diego D

Reputation: 8152

In the most basic way and without doing the sort strategy depending on some query upfront, you can just have a sort callback that first takes into account the property COMPONENT, when they are different, then property PRIORITY and only in the end the equality returning zero.

The point being that if COMPONENT don't differ, the first two criteria are passed and the third becomes the next property comparison.

var data = [
  {COMPONENT: 'PM-ABC', PRIORITY: '0.35'},
  {COMPONENT: 'PM', PRIORITY: '0.35'},
  {COMPONENT: 'PM', PRIORITY: ''}
]

data.sort(function(a, b) {
  if (a.COMPONENT < b.COMPONENT) {
    return -1;
  }
  if (a.COMPONENT > b.COMPONENT) {
    return 1;
  }
  if (a.PRIORITY < b.PRIORITY) {
    return -1;
  }
  if (a.PRIORITY > b.PRIORITY) {
    return 1;
  }
  return 0;
});

console.log(data);

Upvotes: 2

Unmitigated
Unmitigated

Reputation: 89139

You can use String#localeCompare.

let data = [{COMPONENT: 'PM-ABC', PRIORITY: '0.35'},
            {COMPONENT: 'PM', PRIORITY: '0.35'},
            {COMPONENT: 'PM', PRIORITY: ''}];
data.sort((a,b) => a.COMPONENT.localeCompare(b.COMPONENT) ||
  a.PRIORITY.localeCompare(b.PRIORITY));
console.log(data);

Upvotes: 1

Related Questions