![]()
To define a function to sort an Array with String entries, another technique is available. But if the entries to be sorted can be expressed as a number, you may use the numerical sort method described earlier.
For example, to sort the following Array by length of the name:
var names =["Jennifer","John","Ann","Michael","Roger"]
function sortNamesByLength(a,b){
return a.length -b.length;
}
names.sort(sortNamesByLength);
|
function lastLetterInName(a,b){
return a.charCodeAt(a.length-1)-b.charCodeAt(b.length-1);
}
names.sort(lastLetterInName);