Skip to main content

Posts

Showing posts from May, 2014

Decoding the date sorting in Javascript, efficient faster and native way.

Coming from the previous post  (   http://www.developerscloud.org/2014/05/decoding-underscore-js-sortby-sorting.html ) ,  where we have used the underscore to sort the array, with the limitation of sorting in descending order, I will show you in below post how to sort effectively using javascript native functionality. Lets use the same array we have in the previous post. var datesArray = ['6-01-2014','2-01-2014', '4-01-2014', '5-01-2014', '3-01-2014', '1-01-2014']; datesArray is now [" 6-01-2014 ", " 2-01-2014 ", " 4-01-2014 ", " 5-01-2014 ", " 3-01-2014 ", " 1-01-2014 "] Running the for loop making each item of array a date type object. for(var i = 0; i< datesArray.length; i++){ datesArray[i] = new Date(datesArray [i]); } After which datesArray will become [Sun Jun 01 2014 00:00:00 GMT+0530 (India Standard Time), Sat Feb 01 2014 00:...

Decoding underscore js SortBy - Sorting in javascript

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 _.sortBy([6, 2, 4, 5, 3, 1], function(num){ return num }); [ 1 , 2 , 3 , 4 , 5 , 6 ] How to order in Descending Order _.sortBy([6, 2, 4, 5, 3, 1], function(num){ return -num }); [ 6 , 5 , 4 , 3 , 2 , 1 ] The underscore sort by will also work with string literals Ascending _.sortBy(['6','2', '4', '5', '3', '1'], function(num){ return num }); [ " 1 " , " 2 " , " 3 " , " 4 " , " 5 " , " 6 " ] Descending _.sortBy(['6','2'...