58

I'm really struggling on that but I cannot find a solution.

I have an array and I want to sort it by value (all integers). I thought, well let's use lodash, there sure must be a handy function for that.

Somehow I cannot figure out to do this though.

So far I got this:

myArray = [3, 4, 2, 9, 4, 2]

I got a result if I used this code:

myArray = _(myArray).sort();

But unfortunately the return value does not seem to be an array anymore. myArray.length is undefined after the sorting.

I found thousands of examples of lodash sorting array but always via key. https://lodash.com/docs#sortBy

Can somebody tell my how I can get the following return result as an array?:

[2, 2, 3, 4, 4, 9]

It can't be that difficult, but somehow I don't get it done...

Also sometimes I think that lodash documentation is a little complex. I'm probably just missing out an important detail...

Kromster
  • 7,181
  • 7
  • 63
  • 111
Merc
  • 4,241
  • 8
  • 52
  • 81
  • Without lodash, you could just run `myArray = myArray.sort()` – Pete Mar 09 '15 at 20:08
  • @Pete actually using `myArray.sort()` will *not* work for numbers unfortunately as this javascript function actually sorts the numbers as if they are *strings*. So [3, 1, 2, 11234] would be sorted to [1, 11234, 2, 3]. You have to do some stupid arse transformation to make JS sort it as numbers. I recommend using Lodash's _.sortBy() if you can. See reference [here](https://www.w3schools.com/jsref/jsref_sort.asp). – FireDragon Apr 14 '18 at 01:35
  • 1
    @FireDragon I see that you are not insulting a person. But be careful with your langauge anyway please. – Yunnosch Apr 14 '18 at 01:37

2 Answers2

93

You can use the sortBy() function here. You don't have to specify a key, as it will fall back to identity().

var myArray = [ 3, 4, 2, 9, 4, 2 ];

_.sortBy(myArray);
// → [ 2, 2, 3, 4, 4, 9 ]

_(myArray).sortBy().take(3).value();
// → [ 2, 2, 3 ]
Adam Boduch
  • 11,023
  • 3
  • 30
  • 38
  • Ok. thanks a lot. This is working for me. I also got a result by taking this code, although I don't really understand it and this kind of notation is nowhere written down in the lodash docs. `myArray = _(myArray).sort().value();` – Merc Mar 10 '15 at 06:56
  • 1
    Lodash adds array methods to wrappers, and it's mentioned [here](https://lodash.com/docs#_) in the documentation. – Adam Boduch Mar 10 '15 at 11:54
0

Selected Answer is right but we can also do that task with sort()

const _ = require('lodash');
const myArray = [1,2,3,4,"A1","A10","A11","A12","A2","A3","A4","AZ","A5","B10", "B2", "F1", "F12", "F3",1,5,6,7,0,"a","b","a1"];

const sortFilter = _(myArray).sort().value();
console.log(sortFilter)

const sortByFilter = _(myArray).sortBy().value();
console.log(sortByFilter)
Renish Gotecha
  • 2,232
  • 22
  • 21
  • 2
    Using .sort() on the chain method is apparently using the underlying Array sort, which stringifies values before sorting. Which gives some surprising results, like _([1, 2, 10]).sort().value() returns [ 1, 10, 2 ]. – leedm777 Feb 26 '21 at 14:06