Sorting can be the developers worst nightmare especially if it is done in more then one field.
So what can help, recently doing research on it I came across underscore js sortBy (underscorejs.org/#sortBy) function which will help you to sort the array very effectively.
So I was going through the documentation and found it very useful, but there are one drawback of it.
Will let you know that:
How to order in Ascending Order
How to order in Descending Order
The underscore sort by will also work with string literals
Ascending
Descending
The only thing it wont work with is DATES in descending order
Ascending, it works just fine.
_.sortBy(['6-01-2014','2-01-2014', '4-01-2014', '5-01-2014', '3-01-2014', '1-01-2014'], function(num){ return num });
"1-01-2014", "2-01-2014", "3-01-2014", "4-01-2014", "5-01-2014", "6-01-2014"]
Descending, it sucks.
_.sortBy(['6-01-2014','2-01-2014', '4-01-2014', '5-01-2014', '3-01-2014', '1-01-2014'], function(num){ return -num });
["6-01-2014", "2-01-2014", "4-01-2014", "5-01-2014", "3-01-2014", "1-01-2014"]
So to overcome the date sorting, I have another way also, will let you know that in next post
Meanwhile you can go through underscorejs, its awesome
An Introduction to Underscore as per their official site
Underscore is a JavaScript library that provides a whole mess of useful functional programming helpers without extending any built-in objects. It’s the answer to the question: “If I sit down in front of a blank HTML page, and want to start being productive immediately, what do I need?” … and the tie to go along with jQuery's tux andBackbone's suspenders.
Underscore provides 80-odd functions that support both the usual functional suspects:map, select, invoke — as well as more specialized helpers: function binding, javascript templating, deep equality testing, and so on. It delegates to built-in functions, if present, so modern browsers will use the native implementations of forEach, map,reduce, filter, every, some and indexOf.
Continuing post: http://www.developerscloud.org/2014/05/decoding-date-sorting-in-javascript.html
So what can help, recently doing research on it I came across underscore js sortBy (underscorejs.org/#sortBy) function which will help you to sort the array very effectively.
So I was going through the documentation and found it very useful, but there are one drawback of it.
Will let you know that:
How to order in Ascending Order
How to order in Descending Order
The underscore sort by will also work with string literals
Ascending
Descending
The only thing it wont work with is DATES in descending order
Ascending, it works just fine.
_.sortBy(['6-01-2014','2-01-2014', '4-01-2014', '5-01-2014', '3-01-2014', '1-01-2014'], function(num){ return num });
"1-01-2014", "2-01-2014", "3-01-2014", "4-01-2014", "5-01-2014", "6-01-2014"]
Descending, it sucks.
_.sortBy(['6-01-2014','2-01-2014', '4-01-2014', '5-01-2014', '3-01-2014', '1-01-2014'], function(num){ return -num });
["6-01-2014", "2-01-2014", "4-01-2014", "5-01-2014", "3-01-2014", "1-01-2014"]
So to overcome the date sorting, I have another way also, will let you know that in next post
Meanwhile you can go through underscorejs, its awesome
An Introduction to Underscore as per their official site
Underscore is a JavaScript library that provides a whole mess of useful functional programming helpers without extending any built-in objects. It’s the answer to the question: “If I sit down in front of a blank HTML page, and want to start being productive immediately, what do I need?” … and the tie to go along with jQuery's tux andBackbone's suspenders.
Underscore provides 80-odd functions that support both the usual functional suspects:map, select, invoke — as well as more specialized helpers: function binding, javascript templating, deep equality testing, and so on. It delegates to built-in functions, if present, so modern browsers will use the native implementations of forEach, map,reduce, filter, every, some and indexOf.
Continuing post: http://www.developerscloud.org/2014/05/decoding-date-sorting-in-javascript.html
Comments
Post a Comment
Important - Make sure to click the Notify Me check-box below the comment to be notified of follow up comments and replies.