ronghaoZHI/ronghaoZHI.github.io

JS中sort()排序、ACSII码与字符的转换

Opened this issue · 0 comments

JS中的 sort() 方法

Array .prototype .sort()

语法:arr .sort()、arr .sort(compareFunction) //compareFunction 可选的... 指定定义排序顺序的函数。如果省略,则根据每个字符的Unicode代码点值,根据每个元素的字符串转换对数组进行排序。
返回值 //排序的数组。请注意,阵列是按位进行排序的,没有复制。
image

> var fruit = ['cherries', 'apples', 'bananas'];
> fruit.sort(); // ['apples', 'bananas', 'cherries']
> 
> var scores = [1, 10, 21, 2]; 
> scores.sort(); // [1, 10, 2, 21]
> // Note that 10 comes before 2,
> // because '10' is mix of two characters '1' and '0' so '10' is before '2' in Unicode code point order.
> 
> var things = ['word', 'Word', '1 Word', '2 Words'];
> things.sort(); // ['1 Word', '2 Words', 'Word', 'word']
> // In Unicode, numbers come before upper case letters,
> // which come before lower case letters.
> var numbers = [4, 2, 5, 1, 3];
> numbers.sort(function(a, b) {
>   return a - b;
> });
> console.log(numbers);
> 
> // [1, 2, 3, 4, 5]

对象可以根据其属性之一的值进行排序
image

charCodeAt()

str.charCodeAt(index) //index大于或等于0并小于字符串长度的整数; 如果它不是一个数字,它默认为0
//返回值...表示给定索引处字符的UTF-16码单位值的数字; NaN 如果index超出范围
image

formCharCode()

String.fromCharCode(num1[, ...[, numN]])
image