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...
myArray = myArray.sort()
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.