FreeCodeCampChina / FreeCodeCampChina/freecodecamp.cn
Question on array.sort()
- Dominant language
- CSS
- Stars
- 37.8k
- Forks
- 1.4k
- PR merge metrics
- No merged PRs in 30d
Description
Challenge [Where do I belong](https://www.freecodecamp.cn/challenges/where-do-i-belong#?solution=function%20where(arr%2C%20num)%20%7B%0A%20%20%2F%2F%20%E8%AF%B7%E6%8A%8A%E4%BD%A0%E7%9A%84%E4%BB%A3%E7%A0%81%E5%86%99%E5%9C%A8%E8%BF%99%E9%87%8C%0A%20%20arr.sort(function(a%2Cb)%7B%0A%20%20%20%20return%20a%20-%20b%3B%0A%20%20%7D)%3B%0A%20%20for(var%20i%3D0%3B%20i%3Carr.length%3B%20i%2B%2B)%7B%0A%20%20%20%20if(arr%5Bi%5D%20%3E%3D%20num)%7B%0A%20%20%20%20%20%20return%20i%3B%0A%20%20%20%20%7D%0A%20%20%7D%0A%20%20return%20arr.length%3B%0A%7D%0A%0Awhere(%5B5%2C3%2C20%2C3%5D%2C%203)%3B%0A) has an issue.
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.
Please describe how to reproduce this issue, and include links to screenshots if possible.
My code:
```javascript
function where(arr, num) {
// 请把你的代码写在这里
arr.sort(function(a,b){
return a - b;
});
for(var i=0; i= num){
return i;
}
}
return arr.length;
}
where([5,3,20,3], 3);
```
Did I have to write a comparable function to sort a number array ?
I found that `arr = [4, 2, 5]; arr.sort(function(a, b){return a - b});` works.
But Why `arr = [4, 2, 5]; arr.sort();` won't work ?
Contributor guide
Assessment
This issue has not been assessed yet.