Natural Sort in MySQL

后端 未结 21 1364
南旧
南旧 2020-11-22 02:25

Is there an elegant way to have performant, natural sorting in a MySQL database?

For example if I have this data set:

  • Final Fantasy
  • Final Fant
21条回答
  •  遇见更好的自我
    2020-11-22 03:09

    1. Add a Sort Key (Rank) in your table. ORDER BY rank

    2. Utilise the "Release Date" column. ORDER BY release_date

    3. When extracting the data from SQL, make your object do the sorting, e.g., if extracting into a Set, make it a TreeSet, and make your data model implement Comparable and enact the natural sort algorithm here (insertion sort will suffice if you are using a language without collections) as you'll be reading the rows from SQL one by one as you create your model and insert it into the collection)

提交回复
热议问题