Created
October 26, 2012 05:42
-
-
Save hengkiardo/3957079 to your computer and use it in GitHub Desktop.
Custom Javascript array sorting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sort = function(array) { | |
var len = array.length; | |
if(len < 2) { | |
return array; | |
} | |
var pivot = Math.ceil(len/2); | |
return merge(sort(array.slice(0,pivot)), sort(array.slice(pivot))); | |
}; | |
var merge = function(left, right) { | |
var result = []; | |
while((left.length > 0) && (right.length > 0)) { | |
if(left[0]["obj"].someProp > right[0]["obj"].someProp) { | |
result.push(left.shift()); | |
} | |
else { | |
result.push(right.shift()); | |
} | |
} | |
result = result.concat(left, right); | |
return result; | |
}; | |
var largeList = sort(largeList); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment