Created
October 25, 2016 04:58
-
-
Save d30jeff/f14530a00af6ab0143734e6454730103 to your computer and use it in GitHub Desktop.
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 shortArr = [1, 2, 3, 5, 4, 8, 7, 6]; | |
var longArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 22, 24, 23, 28, 27, 26, 25, 24, 22]; | |
function reSortArray(arr) { | |
var limit = 20; | |
if (arr.length < limit) { | |
return arr.sort(); | |
} else { | |
var originalPart = arr.slice(0, arr.length - (arr.length - limit) ); | |
var endPart = arr.slice( 0 - limit - 1).sort(); | |
} | |
return originalPart.concat(endPart); | |
} | |
console.time('short'); | |
console.log(reSortArray(shortArr)); | |
console.timeEnd('short') | |
console.time('long'); | |
console.log(reSortArray(longArr)); | |
console.timeEnd('long'); | |
console.time('native') | |
console.log(longArr.sort(function(a, b) { | |
return parseFloat(a) - parseFloat(b); | |
})); | |
console.timeEnd('native'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment