If I have the following array:
var num = [10% cats, 20% dogs, 30% fish, 40% turtles];
where a pct value is always concatenated to a label. Is there an easy way to ort from largest percentage to smallest? Unlike other similar questions, the format here is always xx% label
using .sort() in the usual .sort(function(a,b) {return b-a;}): doesn't work since these are not numerals?
output should be:
num = [40% turtles, 30% fish, 20% dogs, 10% cats];
.sort((a,b) => Number(b.split('%')[0]) - Number(a.split('%')[0]))- that assumes thatvar numis fixed to be a valid array of strings['10% cats', '20% dogs', '30% fish', '40% turtles'].sort((a,b) => parseFloat(b) - parseFloat(a))