Created
May 2, 2016 14:44
-
-
Save tmcw/d653e3253b6f55e08bb1220414b16984 to your computer and use it in GitHub Desktop.
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 arr = [ | |
1, | |
5, | |
3, | |
[4, 2, 1] | |
]; | |
function sortLevel(list) { | |
list.forEach(function(item) { | |
if (Array.isArray(item)) { | |
sortLevel(item); | |
} | |
}); | |
return list.sort(function(a, b) { | |
var as = a.toString(); | |
var bs = b.toString(); | |
if (as === bs) { | |
return 0; | |
} | |
if (as > bs) { | |
return 1; | |
} else { | |
return -1; | |
} | |
}); | |
} | |
console.log(sortLevel(arr)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment