-
Notifications
You must be signed in to change notification settings - Fork 3.3k
第 43 题:使用 sort() 对数组 [3, 15, 8, 29, 102, 22] 进行排序,输出结果 #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
[3, 15, 8, 29, 102, 22].sort(function(a,b) { |
[3, 15, 8, 29, 102, 22].sort((a, b) => b - a) // [102, 29, 22, 15, 8, 3] |
|
[3, 15, 8, 29, 102, 22].sort((a,b)=>b-a) |
[3, 15, 8, 29, 102, 22].sort((a,b) => {return a - b;}); |
let arr = [3, 15, 8, 29, 102, 22] |
[3, 15, 8, 29, 102, 22] .sort((a,b) => a-b) // [3,8,15,22,29,102] |
[3, 15, 8, 29, 102, 22].sort();
// [102, 15, 22, 29, 3, 8]
[3, 15, 8, 29, 102, 22].sort((a,b) => {return a - b});
|
let arr = [3, 15, 8, 29, 102, 22]; |
let ary = [3, 15, 8, 29, 102, 22] |
arr.sort((a,b) =>{ |
arr.sort((a,b)=>a-b) |
应该默认没有回调函数的,[102, 15, 22, 29, 3, 8] |
|
[3, 15, 8, 29, 102, 22].sort((a,b) => a-b) |
|
`/* dailyIssue:1 } let a=new Array(); |
var arr = [3, 15, 8, 29, 102, 22];
arr.sort()
// [102, 15, 22, 29, 3, 8] arr.sort([compareFunction]) |
Array.prototype.sortAsc=function(){return this.sort((a,b)=>a-b)}; |
let arr = [2, 15, 8, 29, 102, 22]; |
|
从小到大 |
补充一个奇怪的,在新版本的chrome和node中, 这样是不能排序的 [3, 15, 8, 29, 102, 22].sort((a,b) => (b > a)) Safari中是OK的 |
[3, 15, 8, 29, 102, 22].sort( (a,b) =>{ |
漂亮 小姐姐 |
|
你们真是一群妖怪 |
[3,15,8,29,102,22].sort((a,b)=>a-b) |
|
看起来头像对点赞数影响很大 |
[3, 15, 8, 29, 102, 22].sort((a,b)=>{ |
这种用法是不符合规范的,compareFunction 的返回值会和0作比较,引用MDN https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/sort 如果没有指明 compareFunction ,那么元素会按照转换为的字符串的诸个字符的Unicode位点进行排序。例如 "Banana" 会被排列到 "cherry" 之前。当数字按由小到大排序时,9 出现在 80 之前,但因为(没有指明 compareFunction),比较的数字会先被转换为字符串,所以在Unicode顺序上 "80" 要比 "9" 要靠前。 如果指明了 compareFunction ,那么数组会按照调用该函数的返回值排序。即 a 和 b 是两个将要被比较的元素:
|
02-使用sort() 对数组进行排序 - 【3,15,8,29,102,22】
采用的 let arrs = ['你好啊','HELLO','hello',666]
arrs.sort()
console.log(arrs) // [666, "HELLO", "hello", "你好啊"] 总结 数字》英语大写》英语小写》汉字 /**
* Sorts an array.
* @param compareFn Function used to determine the order of the elements. It is expected to return
* a negative value if first argument is less than second argument, zero if they're equal and a positive
* value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.
* ```ts
* [11,2,22,1].sort((a, b) => a - b)
* ```
*/
sort(compareFn?: (a: T, b: T) => number): this;
arr.sort((x, y) => {
console.log(`排序:${x}----${y}`);
});
arr.sort((x, y) => {
console.log(`${x}-${y}=${x - y}`);
});
arr.sort((x, y) => {
console.log(`${x}-${y}=${x - y}`);
return x - y;
});
console.log(arr);
|
sort(): [102, 15, 22, 29, 3, 8] |
[3, 15, 8, 29, 102, 22].sort(new Function( |
输出["a", "b", "c", "d", "o"],因为没有函数时会进行UTF-16编码的比较,小的排前面,
a-b小于0时,会把a和b的位置互换,所以这样排序会把大的数放后面,小的放前面 |
|
这是来自QQ邮箱的假期自动回复邮件。你好,我最近正在休假中,无法亲自回复你的邮件。我将在假期结束后,尽快给你回复。
|
Uh oh!
There was an error while loading. Please reload this page.
原题目:
我的答案:
[102, 15, 22, 29, 3, 8]
解析:
根据MDN上对
Array.sort()
的解释,默认的排序方法会将数组元素转换为字符串,然后比较字符串中字符的UTF-16编码顺序来进行排序。所以'102'
会排在'15'
前面。以下是MDN中的解释原文:The text was updated successfully, but these errors were encountered: