Problems ordering sqlite by a column with accented characters (Á)

后端 未结 2 1861
悲&欢浪女
悲&欢浪女 2020-12-06 17:50

I have a sqlite database, and i need to order by a column that haves some words starting by a accented character. These items are being ordered wrong, they are appearing on

2条回答
  •  庸人自扰
    2020-12-06 18:01

    To support accented characters at database you can use the Normalizer class to store the text properly:

    import java.text.Normalizer;
    
    ...
    
    // Code to store the normalized data
    ContentValues values = new ContentValues();
    values.put(COLUMN_NAME, Normalizer.normalize(name, Normalizer.Form.NFD));
    
    ...
    
    // Code to read the normalized data
    int indexName = cursor.getColumnIndex(COLUMN_NAME)
    String name = Normalizer.normalize(cursor.getString(indexName), Normalizer.Form.NFC));
    

    Storing the data in this way, the SQLite statemants ASC and DESC works properly with accented characters.

提交回复
热议问题