根据末位数字, 对人员进行排序
order("张三3 李四2 王五5");
function order(words) { var reg = /[^\d]+/g, original = words.split(' '), correlation = [], reorder = []; if (words === '') { return words; } original.forEach(function(ele, index) { correlation[index] = {}; correlation[index]['i'] = index; correlation[index]['v'] = Number(ele.replace(reg, '')); }); correlation.sort(function(a, b) { return a.v - b.v; }); correlation.forEach(function(ele, index) { reorder.push(original[ele.i]); }); return reorder.join(' '); }
return "李四2 张三3 王五5":