字母排序

134 阅读1分钟

语法

string.charCodeAt(index)

参数值

参数

描述

index

必需。表示字符串中某个位置的数字,即字符在字符串中的下标。

返回值

类型

描述

Number

返回在指定的位置的字符的 Unicode 编码。

代码片段

resault.sort((a,b) => {
        return a.letter.charCodeAt(0) - b.letter.charCodeAt(0);
});

主要是用 charCodeAt(0) 对比 Unicode编码

charCodeAt() 方法可返回指定位置的字符的 Unicode 编码,返回值是 0 - 65535 之间的整数,表示给定索引处的 UTF-16 代码单元。