When there are many options in a list, users need to slide the window to search for the target option. Sorting by indexes is here to help them quickly find the target option by way of creating an index for each option. It is actually a kind of labeling. For example, labels A, B, C, and D on the right of the contact page correspond to the initial letters of contact names. If you want to find John, clicking J will direct you to a list of names starting with J. Such labels are called indexes, which help to quickly navigate to the corresponding range and find the target option.
For details about how to use related APIs, see IndexUtil.
Import the required module.
import { i18n } from '@kit.LocalizationKit'; Obtain the index list and index value.
// Create index.
let indexUtil: i18n.IndexUtil = i18n.getInstance('zh-CN');
let indexList = indexUtil.getIndexList(); // indexList = ['...', 'A', 'B', 'C', ... 'X', 'Y', 'Z', '...']
// Multi-language index sorting.
indexUtil.addLocale('ru-RU');
// indexList = ['...', 'A', 'B', 'C', ... 'X', 'Y', 'Z', '...', 'А', 'Б', 'В', ... 'Э', 'Ю', 'Я', '...']
indexList = indexUtil.getIndexList();
// Obtain the index value of a string.
let index = indexUtil.getIndex('Hello'); // index = 'N'