openharmony 鸿蒙 i18n-sorting-local

2025-06-12 浏览 (1)

Sorting by Local Habits

Use Cases

The sorting function allows list content, for example, the language list in Settings, to be sorted and displayed in line with local user habits.

How to Develop

You can implement the sorting function by using the compare API of the Collator class. The development procedure is as follows:

  1. Import the intl module.

    import { intl } from '@kit.LocalizationKit';
    
  2. Create a Collator object. You can use CollatorOptions to set different sorting formats. For details, see Table 1.

    let collator: intl.Collator = new intl.Collator(locale: string|Array<string>, options?: CollatorOptions);
    
  3. Compare two strings.

    let compareResult: number = collator.compare(first: string, second: string);
    // If compareResult is a negative number, the first parameter is placed before the second parameter.
    // If compareResult is 0, the first and second parameters are not sorted in sequence.
    // If compareResult is a positive number, the first parameter is placed after the second parameter.
    

Sorting Format Options

Table 1 Supported sorting formats and display effects

NameValueDescriptionDisplay Effect
localeMatcherlookupFuzzy matching.
best fitExact matching.
usagesortSorting.
searchSearch for matched strings.
sensitivitybaseCompare different letters.'a' ≠ 'b', 'a' = 'á', 'a' = 'A'
accentCompare different letters or accents of the same letters.'a' ≠ 'b', 'a' ≠ 'á', 'a' = 'A'
caseCompare the capitalization of different letters or the same letters.'a' ≠ 'b', 'a' = 'á', 'a' ≠ 'A'
variantCompare different letters or accents, and other distinctive signs or capitalization.'a' ≠ 'b', 'a' ≠ 'á', 'a' ≠ 'A'
ignorePunctuationtrueIgnore punctuation.'a,b' = 'ab'
falseNot ignore punctuation.'a,b' < 'ab'
numerictrueSort by number.'1' < '2' < '10' < '11'
falseNot sort by number.'1' < '10' < '11' < '2'
caseFirstupperPlace uppercase letters in the front.'AB' < 'Ab' < 'aB' < 'ab'
lowerPlace lowercase letters in the front.'ab' < 'aB' < 'Ab' < 'AB'
falseNot distinguish first letter capitalization.'ab' < 'aB' < 'Ab' < 'AB'
collationbig5hanPinyin sorting for Latin letters.
compatCompatibility sorting, only for Arabic.
dictDictionary-style sorting, only for Singhalese.
directBinary code sorting.
ducetDefault Unicode collation element table.
eorEuropean sorting.
gb2312Pinyin sorting, only for Chinese.
phonebkPhonebook-style sorting.
phoneticPhonetic sorting.
pinyinPinyin sorting.
reformedReformed sorting, only for Swedish.
searchjlSpecial collation type for Korean initial consonant search.
strokeStroke sorting for Chinese.
tradTraditional-style sorting, for example, Spanish.
unihanRadical-stroke sorting for Han characters, only for Chinese, Japanese, and Korean.
zhuyinZhuyin sorting, only for Chinese.

Development Example

// Import the i18n module.
import { intl } from '@kit.LocalizationKit';

// Create a Collator object.
let options: intl.CollatorOptions = {
  localeMatcher: 'lookup',
  usage: 'sort',
  sensitivity: 'case' // Case sensitive
};
let collator: intl.Collator = new intl.Collator('zh-CN', options);

// Case-sensitive sorting
let array: Array<string> = ['app', 'App', 'Apple', 'ANIMAL', 'animal', 'apple', 'APPLE'];
array.sort((a, b) => { // After sorting: array = ['animal', 'ANIMAL', 'app', 'App', 'apple', 'Apple', 'APPLE']
  return collator.compare(a, b);
})

// Pinyin sorting for Chinese
array = ['Pingguo', 'Li', 'Xiangjiao', 'Shiliu', 'Ganzhe', 'Putao', 'Juzi'];
array.sort((a, b) => { // After sorting: array = ['Ganzhe', 'Juzi', 'Li', 'Pingguo', 'Putao', 'Shiliu', 'Xiangjiao']
  return collator.compare(a, b);
})

// Stroke sorting
options = {
  localeMatcher: 'lookup',
  usage: 'sort',
  collation: 'stroke'
};
let strokeCollator: intl.Collator = new intl.Collator('zh-CN', options);
array = ['Pingguo', 'Li', 'Xiangjiao', 'Shiliu', 'Ganzhe', 'Putao', 'Juzi'];
array.sort((a, b) => { // After sorting: array = ['Ganzhe', 'Shiliu', 'Pingguo', 'Xiangjiao', 'Li', 'Putao', 'Juzi']
  return strokeCollator.compare(a, b);
})

// Search for the matched string.
options = {
  usage: 'search',
  sensitivity: 'base'
};
let searchCollator: intl.Collator = new intl.Collator('tr', options);
array = ['Türkiye', 'TüRkiye', 'salt', 'bright'];
let target: string = 'türkiye';
// matches = ['Türkiye', 'TüRkiye']
let matches: string[] = array.filter(item => searchCollator.compare(item, target) === 0); 

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Localization Kit

harmony 鸿蒙Calendar Setting

harmony 鸿蒙Character Processing

harmony 鸿蒙Overview

harmony 鸿蒙DST Transition

harmony 鸿蒙Overview of Internationalization and Localization

harmony 鸿蒙Language and Locale Name Localization

harmony 鸿蒙Locale ID and Cultural Habit Division

harmony 鸿蒙Number and Unit of Measurement Formatting

harmony 鸿蒙Phone Number Formatting

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/Zfz8VH