I'm using Javascript sort
(with Underscore.js):
_.sortBy(["Bob", "Mary", "Alice"], function (name) {return name}) > ["Alice", "Bob", "Mary"]
I would like the array to return the other way. How do I do that?
["Mary", "Bob", "Alice"]
I don't want to reverse it after it's sorted - I want it to be created the other way around the first time.
Thanks.
Adding Underscore to a Node. Once added, underscore can be referred in any of the Node. js modules using the CommonJS syntax: var _ = require('underscore'); Now we can use the object underscore (_) to operate on objects, arrays and functions.
sortBy() function is used to sort all the elements of the list in ascending order according to the function given to it as a parameter. Passing the array with a function which returns the number and it will sort the array in ascending order and return an array. The array can be both of numeric values and string values.
It provides utility functions for a variety of use cases in our day-to-day common programming tasks. Underscore. js provides a lot of features that make our task easy to work with objects. It can be used directly inside a browser and also with Node.
In JavaScript there is no real privacy of classes. It means that you should not use these method (starting with " _ ") out of your object.
The Underscore.js is a JavaScript library that provides a lot of useful functions that helps in the programming in a big way like the map, filter, invoke etc even without using any built-in objects. The _.sortBy () function is used to sort all the elements of the list in ascending order according to the function given to it as a parameter.
Underscore.js is a lightweight JavaScript library and not a complete framework that was written by Jeremy Ashkenas. It provides utility functions for a variety of use cases in our day-to-day common programming tasks. Underscore.js provides a lot of features that make our task easy to work with objects.
In JavaScript, arrays already have a built in sort function, so why the need for an Underscore sortBy function? Well, the sortBy function has just a bit more granularity associated with it. This first example makes use of an oddSorter function which we can pass as the second argument to the sortBy () function to customize how sorting happens.
Normally underscore.js provides different kinds of functions for collection such as each map and reduce. The functions we can apply to the collection, also provide different methods such as groupBy, countBy, min, and max.
Instead of throwing underscorejs away, I'd rather use it together with Array.reverse
to utilize the best of both.
_.sortBy(["Bob", "Mary", "Alice"], function (name) {return name}) .reverse()
I would just do what Underscore does under the hood: use the Array#sort method.
["Bob", "Mary", "Alice"].sort(function (a, b) { if (a < b) return 1; if (b < a) return -1; return 0; });
Or if you don't want the original array modified, clone it first:
_.clone(["Bob", "Mary", "Alice"]).sort(...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With