慕沐2107578
2022-07-29 17:42
“若返回值<=-1,则表示 a="" b="">-1 &&<1,则表示 a="" b="">=1,则表示 A 在排序后的序列中出现在 B 之后” 这种说法不对吧,不应该是>0和<0区别?
F12打开控制台直接输入以下代码
//当排序的元素都为数字时可以用0 作为分界线
var arr1 = [1,1.9,1.2];
arr1.sort(); //默认升序 [1, 1.2, 1.9]
function sortNum1(n1,n2){return n1-n2}
function sortNum2(n1,n2){return n2-n1}
arr1.sort(sortNum1); //[1, 1.2, 1.9]
arr1.sort(sortNum2); //[1.9, 1.2, 1]
//当排序的元素涉及到特殊字符时,0就不够用了
var arr2 = [1,'3','a',2,'b','&'];
arr2.sort(); //默认['&', 1, 2, '3', 'a', 'b']
function sortUnicode(code1,code2){debugger; return code1-code2}
arr2.sort(sortUnicode);//['&', 1, 2, '3', 'a', 'b']
//1 - '&' NaN '&',1
//2 - 1 1 1,2
//'3' - 2 1 2,'3'
//'a' - '3' NaN '3','a'
//'b' - 'a' NaN 'a','b'
//function sortUnicode(code1,code2){debugger; return code2-code1}
//['3', 2, 1, 'a', 'b', '&']JavaScript进阶篇
469185 学习 · 22584 问题
相似问题