Example:
console.log(f(['12dh', '8-4', '66']))
//output should be => ['8-4', '66', '12dh']
Example:
console.log(f(['12dh', '8-4', '66']))
//output should be => ['8-4', '66', '12dh']
You can do this very easily with sort
and reverse
.
var array = ['12dh', '8-4', '66'];
var sorted = array.sort().reverse();
console.log(sorted);
You can also wrap it in a function.
function sortReverse(array){
return array.sort().reverse();
}
Also if you're concerned about performance you can pass a custom comparison function to sort
:
array.sort(function(a,b){
return a < b;
});
This could be quicker when dealing with larger arrays, because it doesn't have to reverse after sorting.
To filter anything that doesn't start with a number:
array.filter(function(str){
return !isNaN(str.charAt(0));
}).sort().reverse();
You can do something like that:
var array = ['12dh', '8-4', '66'];
array.sort(function (a, b) {
if (a > b) {
return -1;
}
if (a < b) {
return 1;
}
return 0;
});
I hope that helps :D