So I have come to find that I like the lodash _.sortBy method more so than the native Array.prototype.sort method for a few various reasons. I do still use it of course when it comes to working with a project where lodash is not part of the stack, it is just that the method works in a way that I find more natural when it comes to the return value and arguments used for the function that is passed to the sort by method. I will be elaborating what I mean by that in this post.
For a basic example of _.sortBy why not start off with just using it to sort an array of numbers. It does not get more basic than that does it. To sort an array of numbers by numerical value, I just have to give the array of numbers to _.sortBy. No need to give and method as the second argument.
This is all fine and good for a very basic example of the lodash sort by method, but what if I want to set some kind of condition for sorting? A function can of course be given as a second argument, so lets look at another basic example of that.
I can give a method as a second argument that can be used to define an expression for sorting rather than using the default sort behaviors that might not always work the way I want it to. Inside the body of the function that I give to sort by the first argument will be a given array element value, or the value of a named object key in the event of a named collection object of some kind. When it comes to the return value in which situations the return value should be a number and the lower the number is the closer to an index value of zero the value will be in the resulting array returned by the lodash sortBy method.
That is all fine and good, but in many projects I am working with an array of objects. So lets look at some more basic examples.
One of the great things about the lodash sort by method is that this sort by method is one of the many examples of a collection method in lodash. What I mean by that is that the lodash sort by method will not just work with arrays, but with objects in general including objects that are not even array like objects. So then I can pass any kind of object that I would want to sort to the sort by method including objects with named rather than numbed public key names.
So then because the sort by method and other lodash methods like the map method are collection methods this helpers with the process of doing things with collections in general such as creating an object over again, but with the named keys sorted in a new away. That is that when using a method like the lodash keys method to get an array of key names of an object the order of the key names is consistent with the order in which they are defined when creating the object. If I want to create the same object again, but with the keys sorted in a new order by way of the value rather than the name of the keys this will require a little leg work.
This example then makes use of the lodash chain method to preform a few tasks with various lodash methods on an object in this case a collection with named key values.
When it comes to an array of objects a property name can be given as a second argument to a number value that will be used t sort the collection. If that does not cut it a function can be given to create and return a numbered value that will be used to sort the collection.
When it comes to finding an item in a collection there is finding a single item, and then there is sorting the collection and taking the top or bottom item of that collection. The lodash _.find method will work okay in most situations depending on the nature of the condition that is used. In some cases it would be better to sort the collection by a condition, and then take the first element.
For example say I have a bunch of blog posts and I want to find the post in my collection of posts that has the highest word count. Using the lodash _.find method in that case, would not work out so well because the nature of condition must be applied to all items in the collection. It is a situation in which there is a value that is unique to just one item, but that value is not known it must be found first in order to know what I am looking for. So a better alternative would be to sort.
There are a few draw backs to the use of the array sort method one of which is that it will mutate an array in place rather that returning a new array. So the right off the bat there is that problem that makes things a little more complicated if I do not want to mutate in place meaning that I will need to preform a shallow clone of a source array first. Another thing about the array sort method is the default sort that will happen with an array of number primitives rather than sub strings, by default the sorting is preformed as alphabetical rather than that of number values. Most of the sorting that I have to do is with number values, so then I always need to give a custom sort function when using it. Yet another draw back is that the array sort method is simply that, and array method and not a collection method. So then when it comes to sorting named collections that is something that I can not do, at least not with the array sort method by itself anyway.
For this example I am sorting an array of numbers with the array sort method by itself without any custom logic given in the form of a function as an argument for the array sort method. The result of doing so might not always be what one would expect, or yet again maybe it will be depending on what you think the default sorting behavior should be for a method such as this. Simply put the default sorting of primitives will be that of alphabetical rather than numerical value. This might be desired if we are talking about an array of sub strings, but maybe not so much when it comes to an array of numbers.
This might not be a deal breaker when it comes to using the array sort method though as this can easily be resolved by just passing a custom sort function. Which is a practice that should generally always be done when using a method like this away is it is not always such a great idea to leave things to whatever the default is anyway. The mutation in place concern can also be addresses by just making use of one of many little tricks for creating a shallow clone of an array in this case also, so lets look at a few more examples of the array sort method that might help to address some of these concerns.
In order to get the kinds of results that I want with the native array sort by method I am going to need to to give it a custom sort by method. The way that such a method works with the native array sort meth9d is a little different compared to that of the lodash sort by method. The return value of the function should be an element index delta value rather than that of a number value that is used to range the current element. Also there is not one element reference by two as the sorting is preform by making a comparison between two elements in the array, rather than going threw them one by one.