Rahul Shivsharan
Rahul Shivsharan

Reputation: 2561

How to sort on basis of multiple keys using underscore.js?

Lets say I have a table "employeeTbl_1"

If I run the below query

 select * from employeeTbl_1

the output is

 1  Adam        23000
 2  Adam        22000
 3  Annabel     13500
 4  Abraham     13000
 5  Alan        12000
 6  Svatlana    1500
 7  Svatlana    1400
 8  Shane       12000

Now if I sort the above table using "employeeName"

the output is as follows,

select * from employeeTbl_1 order by employeeName

4   Abraham     13000
1   Adam        23000
2   Adam        22000
5   Alan        12000
3   Annabel     13500
8   Shane       12000
6   Svatlana    1500
7   Svatlana    1400

Now if I sort the table using "employeeName" and "salary" both, the output is as follows,

select * from employeeTbl_1 order by employeeName, salary

4   Abraham     13000
2   Adam        22000
1   Adam        23000
5   Alan        12000
3   Annabel     13500
8   Shane       12000
7   Svatlana    1400
6   Svatlana    1500

You can see in the above (i.e. 3rd output) the table is sorted on basis of both the column,

the same I need to implement in javaScript using underscore.js library,

please follow the code below,

 var _ = require("underscore");

 var employeeList = [   {"id" : 1, "name" : "Adam", "salary" : 23000 },
                    {"id" : 2, "name" : "Adam", "salary" : 22000 },
                    {"id" : 3, "name" : "Annabel", "salary" : 13500 },
                    {"id" : 4, "name" : "Abraham", "salary" : 13000 },
                    {"id" : 5, "name" : "Alan", "salary" : 12000 },
                    {"id" : 6, "name" : "Svatlana", "salary" : 1500 },
                    {"id" : 7, "name" : "Svatlana", "salary" : 1400 },
                    {"id" : 8, "name" : "Shane", "salary" : 12000 },
                ];

 function sortEmployee(list){
    return _.sortBy(_.sortBy(list,"name"),"salary");
 }

 console.log(sortEmployee(employeeList));                   

If I run the above code the output is,

 rahul@rahul:~/myPractise/FunctionalJavascriptPractise$ node practise22.js 
 [ { id: 7, name: 'Svatlana', salary: 1400 },
   { id: 6, name: 'Svatlana', salary: 1500 },
   { id: 5, name: 'Alan', salary: 12000 },
   { id: 8, name: 'Shane', salary: 12000 },
   { id: 4, name: 'Abraham', salary: 13000 },
   { id: 3, name: 'Annabel', salary: 13500 },
   { id: 2, name: 'Adam', salary: 22000 },
   { id: 1, name: 'Adam', salary: 23000 } ]
 rahul@rahul:~/myPractise/FunctionalJavascriptPractise$

From the output you can see the collection is sorted on one condition i.e. "salary".

How can I achieve the sort in JavaScript using plain javaScript or using underscore.js as I got in sql.

I tried using plain javaScript and underScore.js

function sortEmployee(list){
   return list.sort(function(employee1,employee2){      
    return _.isEqual(employee1.name,employee2.name) ?
            (_.isEqual(employee1.name,employee2.name) && (employee1.salary < employee2.salary)) : _.isEqual(employee1.name,employee2.name);
    });
}

console.log(sortEmployee(employeeList));

The output is

[ { id: 1, name: 'Adam', salary: 23000 },
  { id: 2, name: 'Adam', salary: 22000 },
  { id: 3, name: 'Annabel', salary: 13500 },
  { id: 4, name: 'Abraham', salary: 13000 },
  { id: 5, name: 'Alan', salary: 12000 },
  { id: 6, name: 'Svatlana', salary: 1500 },
  { id: 7, name: 'Svatlana', salary: 1400 },
  { id: 8, name: 'Shane', salary: 12000 } ]

I am still not getting the desired ourput, which I get in sql sorting

Upvotes: 0

Views: 70

Answers (1)

Meir
Meir

Reputation: 14375

Underscore's sortBy accepts an iteratee function as well, if you pass it a function that first checks the name and if they're equal compares the salary, you'll get the desired sort.

comparator(v1, v2){
   return ((v1.name  - v2.name) || (v1.value - v2.value))
}

If the names are equal, the second clause kicks in and compares the values.

After reading your comment, here is an updated version:

function nameSalaryComparator(v1, v2){
  return (v1.name === v2.name ?
          v1.salary - v2.salary :
          (v1.name > v2.name ? 1 : -1));
}

You can find a working jsbin here. I even added another entry to show it sorts properly.

Your code probably doesn't work cause you return a boolean instead of a number. I can't access MDN's site but you can see a reference here. Your function should return a number that represents the order:

Optional. A function that defines an alternative sort order. The function should return a negative, zero, or positive value, depending on the arguments...

Upvotes: 1

Related Questions