本地习惯排序
使用场景
在用户使用到排序的场景下,提供符合用户使用习惯的排序方法展示内容。例如,设置中“系统和语言”的语言列表,列表需要按照当地用户习惯进行排序。
开发步骤
多语言列表按照本地习惯进行排序,通过Collator类的compare接口实现,具体开发步骤如下。
-
导入模块。
import Intl from '@ohos.intl';
-
创建collator排序对象。 构造函数支持通过CollatorOptions设置不同的排序格式,具体请参考表1。
let collator = new Intl.Collator(locale: string | Array<string>, options?: CollatorOptions);
-
比较字串。
let compareResult = collator.compare("first", "second"); // compareResult 为负数,表示第一个参数排在第二个参数之前 // compareResult 为0,表示第一个参数与第二个参数排序不分先后 // compareResult 为正数,表示第一个参数排在第二个参数之后
排序格式选项
表1 CollatorOptions各参数取值和显示效果
名称 | 取值 | 描述 | 显示效果 |
---|---|---|---|
localeMatcher | lookup | 模糊匹配 | |
best fit | 准确匹配 | ||
usage | sort | 用作排序 | |
search | 用作查找匹配的字符串 | ||
sensitivity | base | 不同的字母比较不相等 | 例如: a ≠ b, a = á, a = A. |
accent | 不同的字母或读音比较不相等 | 例如: a ≠ b, a ≠ á, a = A. | |
case | 不同的字母或同一字母大小写比较不相等 | 例如: a ≠ b, a = á, a ≠ A. | |
variant | 不同的字母或读音及其它有区别的标志或大小写都是不相等的 | 例如: a ≠ b, a ≠ á, a ≠ A. | |
ignorePunctuation | true | 忽略标点 | a,b = ab |
false | 不忽略标点 | a,b < ab | |
numeric | true | 使用数字排序 | 1 < 2 < 10 < 11 |
false | 不使用数字排序 | 1 < 10 < 11 < 2 | |
caseFirst | upper | 大写排前面 | ab,aB, AB,Ab => AB < Ab < aB < ab |
lower | 小写排前面 | ab,aB, AB,Ab => ab < aB < Ab < AB | |
false | 不区分首字母大小写 | ab,aB, AB,Ab => ab < aB < Ab < AB | |
collation | big5han | 拉丁字母使用的拼音排序 | |
compat | 兼容性排序,仅用于阿拉伯语 | ||
dict | 词典风格排序,仅用于僧伽罗语 | ||
direct | 二进制码点排序 | ||
ducet | Unicode排序规则元素表 | ||
eor | 欧洲排序规则 | ||
gb2312 | 拼音排序,仅用于中文排序 | ||
phonebk | 电话本风格排序 | ||
phonetic | 发音排序 | ||
pinyin | 拼音排序 | ||
reformed | 瑞典语排序 | ||
searchjl | 韩语初始辅音搜索的特殊排序 | ||
stroke | 汉语的笔画排序 | ||
trad | 传统风格排序,如西班牙语 | ||
unihan | 统一汉字排序,用于日语、韩语、中文等汉字排序 | ||
zhuyin | 注音排序,仅用于中文排序 |
开发实例
// 导入模块
import I18n from '@ohos.i18n';
import Intl from '@ohos.intl';
// 创建排序对象
let options: Intl.CollatorOptions = {
localeMatcher: "lookup",
usage: "sort",
sensitivity: "case" // 区分大小写
};
let collator = new Intl.Collator("zh-CN", options);
// 区分大小写排序
let array = ["app", "App", "Apple", "ANIMAL", "animal", "apple", "APPLE"];
array.sort((a, b) => {
return collator.compare(a, b);
})
console.log("result: ", array); // animal ANIMAL app App apple Apple APPLE
// 中文拼音排序
array = ["苹果", "梨", "香蕉", "石榴", "甘蔗", "葡萄", "橘子"];
array.sort((a, b) => {
return collator.compare(a, b);
})
console.log("result: ", array); // 甘蔗,橘子,梨,苹果,葡萄,石榴,香蕉
// 按笔画排序
options = {
localeMatcher: "lookup",
usage: "sort",
collation: "stroke"
};
collator = new Intl.Collator("zh-CN", options);
array = ["苹果", "梨", "香蕉", "石榴", "甘蔗", "葡萄", "橘子"];
array.sort((a, b) => {
return collator.compare(a, b);
})
console.log("result: ", array); // 甘蔗,石榴,苹果,香蕉,梨,葡萄,橘子