0

I am sorting an object array by multiple keys using the _.sortBy() method of Javascript Underscore.js. I used the first name and last name inside the names object for sorting.

My Code:

var names = [
  { fname: 'Chloe', lname: 'Bennet' },
  { fname: 'Adam', lname: 'Levine' },
  { fname: 'Chris', lname: 'Hemsworth' },
  { fname: 'Justin', lname: 'Bieber' },
  { fname: 'Chloe', lname: 'Moretz' },
  { fname: 'Taylor', lname: 'Swift' }
];

console.log(_.sortBy(someData, 'fname' + ' ' + 'lname')

Expected result:

{ fname: 'Adam', lname: 'Levine' },
{ fname: 'Chloe', lname: 'Bennet' },
{ fname: 'Chloe', lname: 'Moretz' },
{ fname: 'Chris', lname: 'Hemsworth' },
{ fname: 'Justin', lname: 'Bieber' },
{ fname: 'Taylor', lname: 'Swift' }

But it is not sorting the array.

7
  • so, what's the right result? include it in your question. Commented Jul 10, 2018 at 8:16
  • 2
    Did you consider reading the doc? underscorejs.org/#sortBy Commented Jul 10, 2018 at 8:16
  • 1
    The right answer is in the Expected result Commented Jul 10, 2018 at 8:16
  • Possible duplicate of sortby using Underscore Commented Jul 10, 2018 at 8:17
  • 1
    _.sortBy takes either name of the property(in string) or a function as its second parameter. consider use a function in your case Commented Jul 10, 2018 at 8:21

3 Answers 3

3

You need to provide function for more complex comparisons:

var names = [
  { fname: 'Chloe', lname: 'Bennet' },
  { fname: 'Adam', lname: 'Levine' },
  { fname: 'Chris', lname: 'Hemsworth' },
  { fname: 'Justin', lname: 'Bieber' },
  { fname: 'Chloe', lname: 'Moretz' },
  { fname: 'Taylor', lname: 'Swift' }
];

console.log(_.sortBy(names, ({ fname, lname }) => fname + ' ' + lname))
.as-console-wrapper {max-height: 100% !important;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>

Sign up to request clarification or add additional context in comments.

1 Comment

Great thanks! It's actually working and it's more lesser code than the one I thought.
0

According to the documentation:

var names = [
  { fname: 'Chloe', lname: 'Bennet' },
  { fname: 'Adam', lname: 'Levine' },
  { fname: 'Chris', lname: 'Hemsworth' },
  { fname: 'Justin', lname: 'Bieber' },
  { fname: 'Chloe', lname: 'Moretz' },
  { fname: 'Taylor', lname: 'Swift' }
];
console.log(_.sortBy(names, ['fname', 'lname']));

Regards, Vincent

1 Comment

My bad, I thought you were talking about the lodash library, not underscore.
0

I have come up with my answer. Thanks to everyone for giving me idea.

var names = [
  { fname: 'Chloe', lname: 'Bennet' },
  { fname: 'Adam', lname: 'Levine' },
  { fname: 'Chris', lname: 'Hemsworth' },
  { fname: 'Justin', lname: 'Bieber' },
  { fname: 'Chloe', lname: 'Moretz' },
  { fname: 'Taylor', lname: 'Swift' }
];

console.log(
    _.sortBy(names , 
       function(value) {
         return value.fname + ' ' + value.lname;
       }
    )
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.