开发者社区> 问答> 正文

使用sort()对数组 [3, 15, 8,29,102,22] 进行排序,输出结果。#前端面试

使用 sort() 对数组 [3, 15, 8, 29, 102, 22] 进行排序,输出结果

展开
收起
Bill 2020-05-23 13:52:24 2096 0
1 条回答
写回答
取消 提交回答
  • 领取2折优惠劵,有几率免单哦!http://www.weilai.info/tool/326.html

    答案: image.png 解析: 根据MDN上对Array.sort()的解释,默认的排序方法会将数组元素转换为字符串,然后比较字符串中字符的UTF-16编码顺序来进行排序。所以'102' 会排在 '15' 前面。以下是MDN中的解释原文:

    The sort() method sorts the elements of an array in place and returns the array. The default sort order is built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

    2020-05-24 11:48:57
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
阿里云技术面试红宝书 立即下载
超全算法笔试-模拟题精解合集 立即下载
程序员面试宝典 立即下载