What does COLLATE LOCALIZED ASC stand for?
private Cursor getContacts() {
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;开发者_如何学Go
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +
(mShowInvisible ? "0" : "1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME +
" COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}
What does COLLATE LOCALIZED ASC
stand for?
Collate is just fancy speak for sort (well sort of). So this is sort ordering based on localized preferences (i.e. current language's alphabet and conventions) in ascending order.
It instructs SQLite to sort non-ASCII characters appropriately. Chars with diacritics (some call them accents) have higher byte codes than the character Z, so a plain ASCII sort wouldn't fit to many foreign languages.
For example, the capital A char's byte code is 0x41
and the capital Z char's is 0x5A
. Then we have the Á (capital A accute) which code in Unicode is 0x00C1
. So a plain byte code sort would result the Á to be after the Z.
But in languages that have this kind of characters the convention is to put those with diacritics as if they didn't have the diacritic. So the Á should be together with the plain A, at least before B.
And to illustrate, we have below a list of names sorted using their bytecode:
- Brenda
- Debby
- George
- Álvaro
- Érico
Now using the COLLATE LOCALIZED
it would sort by the "base" of the character:
- Álvaro
- Brenda
- Debby
- Érico
- George
COLLATE is an SQL operator that lets you override the default sort order for strings. For example, "COLLATE NOCASE" does case-insensitive comparison, and "COLLATE BINARY" does a case-sensitive comparison.
The SQLite C interface lets you define custom collations (http://www.sqlite.org/c3ref/create_collation.html).
精彩评论