ORDER BY alphanumeric characters only in SQLite

后端 未结 5 1167
再見小時候
再見小時候 2021-02-05 15:24

I am sorting songs in SQLite (on Android). I want to order them:

  1. Case-insensitive
  2. With leading-digits at the end, by integer value.
  3. Without punct
5条回答
  •  渐次进展
    2021-02-05 16:02

    I would add an additional column in the table, called "SortingName" or something. Calculate this value when inserting, ideally not in SQL but in a higher level language where you have all these nice string operations.

    I didn't really understand this thing with the number. I guess the simplest thing you can do is extract the number before insert and put it into another column, like "SortingNumber".

    Then simply sort like this:

    Order By
      SortingName,
      SortingNumber
    

    (Or the other way around.)

    Another advantage is performance. You usually read data much more often then you write it. You can even create indexes on these two sorting columns, which is usually not possible if you calculate it in the query.

提交回复
热议问题