Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Javascript with Underscore.js to Sort the other way

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.

like image 807
Harry Avatar asked Jan 02 '12 18:01

Harry


People also ask

How do you use underscore in JavaScript?

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.

How do you use sortBy in JavaScript?

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.

Why is JavaScript underscore better?

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.

Should you use underscore js?

In JavaScript there is no real privacy of classes. It means that you should not use these method (starting with " _ ") out of your object.

How to sort list in underscore JS?

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.

What is underscore JS?

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.

Why do I need an underscore sortby function?

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.

What are the different types of functions for collection in underscore?

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.


2 Answers

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() 
like image 183
h--n Avatar answered Oct 08 '22 02:10

h--n


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(...) 
like image 25
Felix Loether Avatar answered Oct 08 '22 02:10

Felix Loether