Sort javascript array by content length
I needed to sort an array by the length of the string values in descending order. JS has a lot of nice features and building your own sort order logic is one of them. To get the list in ascending order, just switch the comparison operator.
function sortByLengthDesc(a, b) {
if (a.length > b.length)
return -1;
if (a.length < b.length)
return 1;
return 0;
}
arrTmp = tmpText.split('<br>');
arrTmp.sort(sortByLengthDesc);
Trackback URI |