Search code examples
iosdatabasesqlitealphabetical

SQlite alphabetical order


I have an SQlite database with a few categories and each of them have some items inside. Right now the categories are arranged in alphabetical order but I want in the order I put them in the database. Any suggestions? I'm pretty new at this and can't figure it out how to get rid of the alphabetical order.


Solution

  • Lets take a look with simple example:

    Table : Categories

    cat_id cat_name someotherValues

      1      A            XYZ
      3      B            UUI
      2      C            8XU
      4      F            6XK
      6      E            9XU
      5      D            7XP
    

    Now, if you want it in alphabetical order, you can query like,

    select * from categories order by cat_id

    It'll return you rows in 1,2,3,..,6 of cat_id order.

    P.S. There's a default row_id column created with SQLite tables. You can set order on row_id if you haven't any unique field to set order. However order can by set on any field.