Skip to content

Instantly share code, notes, and snippets.

@tmcw
Created May 2, 2016 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmcw/d653e3253b6f55e08bb1220414b16984 to your computer and use it in GitHub Desktop.
Save tmcw/d653e3253b6f55e08bb1220414b16984 to your computer and use it in GitHub Desktop.
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