I am sorting songs in SQLite (on Android). I want to order them:
In my opinion, the highest performance approach is to create a trigger to fill a new field named sort_key
. You will need a primary key.
CREATE TABLE songs (n INTEGER, name TEXT,
sort_key TEXT,
ID INTEGER PRIMARY KEY AUTOINCREMENT);
CREATE TRIGGER songs_key_trigger
AFTER INSERT ON songs FOR EACH ROW
BEGIN n
Declare @sort_key as varchar(255)
-- calculate and call here your slugify function
-- to fill sort_key from 'new.n' and 'new.name'
UPDATE songs
SET sort_key = @sort_key
WHERE ID = new.ID;
END
Realize that this approach is index friendly, you can create an index over new column to avoid table full scan operations.