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

后端 未结 2 1856
悲&欢浪女
悲&欢浪女 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:11

    Either

    ... ORDER BY column COLLATE UNICODE
    

    or

    ... ORDER BY column COLLATE LOCALIZED
    

    Reference:

    In addition to SQLite's default BINARY collator, Android supplies two more, LOCALIZED, which changes with the system's current locale, and UNICODE, which is the Unicode Collation Algorithm and not tailored to the current locale.

    Example:

    db.execSQL("CREATE TABLE foo(a TEXT);");
    db.execSQL("INSERT INTO foo VALUES('Antonio'),('Bonzo'),('Zeto'),('Ángela');");
    
    Cursor c = db.rawQuery("SELECT * FROM foo ORDER BY a COLLATE UNICODE", null);
    while (c.moveToNext()) {
       Log.d("foo", c.getString(0));
    }
    

    Output:

    Ángela
    Antonio
    Bonzo
    Zeto
    

提交回复
热议问题