1e41f4b71Sopenharmony_ci# Sorting by Local Habits 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Use Cases 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciThe sorting function allows list content, for example, the language list in **Settings**, to be sorted and displayed in line with local user habits. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## How to Develop 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciThe sorting function is implemented by the [compare](../reference/apis-localization-kit/js-apis-intl.md#compare8) API of the [Collator](../reference/apis-localization-kit/js-apis-intl.md#collator8) class. The development procedure is as follows: 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci1. Import the **intl** module. 12e41f4b71Sopenharmony_ci ```ts 13e41f4b71Sopenharmony_ci import { intl } from '@kit.LocalizationKit'; 14e41f4b71Sopenharmony_ci ``` 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci2. Create a **Collator** object. 17e41f4b71Sopenharmony_ci You can use **CollatorOptions** to set different sorting formats. For details, see Table 1. 18e41f4b71Sopenharmony_ci ```ts 19e41f4b71Sopenharmony_ci let collator = new intl.Collator(locale: string | Array<string>, options?: CollatorOptions); 20e41f4b71Sopenharmony_ci ``` 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci3. Compare two strings. 23e41f4b71Sopenharmony_ci ```ts 24e41f4b71Sopenharmony_ci let compareResult = collator.compare(first: string, second: string); 25e41f4b71Sopenharmony_ci // If compareResult is a negative number, the first parameter is placed before the second parameter. 26e41f4b71Sopenharmony_ci // If compareResult is 0, the first and second parameters are not sorted in sequence. 27e41f4b71Sopenharmony_ci // If compareResult is a positive number, the first parameter is placed after the second parameter. 28e41f4b71Sopenharmony_ci ``` 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci**Sorting Format Options** 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci**Table 1** Supported sorting formats and display effects 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci| Name| Value| Description| Display Effect| 35e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 36e41f4b71Sopenharmony_ci| localeMatcher | lookup | Fuzzy matching.| | 37e41f4b71Sopenharmony_ci| | best fit | Exact matching.| | 38e41f4b71Sopenharmony_ci| usage | sort | Sorting.| | 39e41f4b71Sopenharmony_ci| | search | Search for matched strings.| | 40e41f4b71Sopenharmony_ci| sensitivity | base | Compare different letters.| Example: a ≠ b, a = á, a = A| 41e41f4b71Sopenharmony_ci| | accent | Compare different letters or accents.| Example: a ≠ b, a ≠ á, a = A| 42e41f4b71Sopenharmony_ci| | case | Compare the capitalization of different letters or the same letter.| Example: a ≠ b, a = á, a = A| 43e41f4b71Sopenharmony_ci| | variant | Compare different letters or accents, and other distinctive signs or capitalization.| Example: a ≠ b, a ≠ á, a ≠ A| 44e41f4b71Sopenharmony_ci| ignorePunctuation | true | Ignore punctuation.| a,b = ab | 45e41f4b71Sopenharmony_ci| | false | Not ignore punctuation.| a,b < ab | 46e41f4b71Sopenharmony_ci| numeric | true | Sort by number.| 1 < 2 < 10 < 11 | 47e41f4b71Sopenharmony_ci| | false | Not sort by number.| 1 < 10 < 11 < 2 | 48e41f4b71Sopenharmony_ci| caseFirst | upper | Place uppercase letters in the front.| ab,aB, AB,Ab => AB < Ab < aB < ab | 49e41f4b71Sopenharmony_ci| | lower | Place lowercase letters in the front.| ab,aB, AB,Ab => ab < aB < Ab < AB | 50e41f4b71Sopenharmony_ci| | false | Not distinguish first letter capitalization.| ab,aB, AB,Ab => ab < aB < Ab < AB | 51e41f4b71Sopenharmony_ci| collation | big5han | Pinyin sorting for Latin letters.| | 52e41f4b71Sopenharmony_ci| | compat | Compatibility sorting, only for Arabic.| | 53e41f4b71Sopenharmony_ci| | dict | Dictionary-style sorting, only for Singhalese.| | 54e41f4b71Sopenharmony_ci| | direct | Binary code sorting.| | 55e41f4b71Sopenharmony_ci| | ducet | Default Unicode collation element table.| | 56e41f4b71Sopenharmony_ci| | eor | European sorting.| | 57e41f4b71Sopenharmony_ci| | gb2312 | Pinyin sorting, only for Chinese.| | 58e41f4b71Sopenharmony_ci| | phonebk | Phonebook-style sorting.| | 59e41f4b71Sopenharmony_ci| | phonetic | Phonetic sorting.| | 60e41f4b71Sopenharmony_ci| | pinyin | Pinyin sorting.| | 61e41f4b71Sopenharmony_ci| | reformed | Reformed sorting, only for Swedish.| | 62e41f4b71Sopenharmony_ci| | searchjl | Special collation type for Korean initial consonant search.| | 63e41f4b71Sopenharmony_ci| | stroke | Stroke sorting for Chinese.| | 64e41f4b71Sopenharmony_ci| | trad | Traditional-style sorting, for example, Spanish.| | 65e41f4b71Sopenharmony_ci| | unihan | Radical-stroke sorting for Han characters, only for Chinese, Japanese, and Korean.| | 66e41f4b71Sopenharmony_ci| | zhuyin | Zhuyin sorting, only for Chinese.| | 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci**Development Example** 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci```ts 71e41f4b71Sopenharmony_ci// Import the i18n module. 72e41f4b71Sopenharmony_ciimport { intl } from '@kit.LocalizationKit'; 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci// Create a Collator object. 75e41f4b71Sopenharmony_cilet options: intl.CollatorOptions = { 76e41f4b71Sopenharmony_ci localeMatcher: "lookup", 77e41f4b71Sopenharmony_ci usage: "sort", 78e41f4b71Sopenharmony_ci sensitivity: "case" // Case sensitive 79e41f4b71Sopenharmony_ci}; 80e41f4b71Sopenharmony_cilet collator = new intl.Collator("zh-CN", options); 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci// Case-sensitive sorting 83e41f4b71Sopenharmony_cilet array = ["app", "App", "Apple", "ANIMAL", "animal", "apple", "APPLE"]; 84e41f4b71Sopenharmony_ciarray.sort((a, b) => { 85e41f4b71Sopenharmony_ci return collator.compare(a, b); 86e41f4b71Sopenharmony_ci}) 87e41f4b71Sopenharmony_ciconsole.log("result: ", array); // animal ANIMAL app App apple Apple APPLE 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci// Pinyin sorting for Chinese 90e41f4b71Sopenharmony_ciarray = ["Pingguo", "Li", "Xiangjiao", "Shiliu", "Ganzhe", "Putao", "Juzi"]; 91e41f4b71Sopenharmony_ciarray.sort((a, b) => { 92e41f4b71Sopenharmony_ci return collator.compare(a, b); 93e41f4b71Sopenharmony_ci}) 94e41f4b71Sopenharmony_ciconsole.log("result: ", array); // Ganzhe, Juzi, Li, Pingguo, Putao, Shiliu, Xiangjiao 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci// Stroke sorting 97e41f4b71Sopenharmony_cioptions = { 98e41f4b71Sopenharmony_ci localeMatcher: "lookup", 99e41f4b71Sopenharmony_ci usage: "sort", 100e41f4b71Sopenharmony_ci collation: "stroke" 101e41f4b71Sopenharmony_ci}; 102e41f4b71Sopenharmony_cicollator = new intl.Collator("zh-CN", options); 103e41f4b71Sopenharmony_ciarray = ["苹果", "梨", "香蕉", "石榴", "甘蔗", "葡萄", "橘子"]; 104e41f4b71Sopenharmony_ciarray.sort((a, b) => { 105e41f4b71Sopenharmony_ci return collator.compare(a, b); 106e41f4b71Sopenharmony_ci}) 107e41f4b71Sopenharmony_ciconsole.log("result: ", array); // 甘蔗,石榴,苹果,香蕉,梨,葡萄,橘子 108e41f4b71Sopenharmony_ci``` 109e41f4b71Sopenharmony_ci<!--no_check-->