-2
<!DOCTYPE html>  
<html>
<body>    
  <p id="demo">Click the button to sort the array.</p>  
  <button onclick="myFunction()">Try it</button>  
  <script>
  function myFunction()
  {    
    var fruits = ["Banana", "Orange", "Apple", "Mango","aaaa"];
    fruits.sort();
    var x=document.getElementById("demo");
    x.innerHTML=fruits;
  }    
  </script>    
</body>    
</html>
4

1 回答 1

0

排序是使用charCode字符串中的字符完成的。UTF8 和 ASCII 表在小写字母之前有大写字母:

"A".charCodeAt(0) == 65
"a".charCodeAt(0) == 97

如果要执行不区分大小写的顺序,可以将比较函数传递给sort调用。

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort

看看这个问题:

如何在Javascript中执行不区分大小写的排序?

于 2013-03-11T09:19:49.847 回答