Natural Sort in MySQL

后端 未结 21 1085
南旧
南旧 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:04

    Add a field for "sort key" that has all strings of digits zero-padded to a fixed length and then sort on that field instead.

    If you might have long strings of digits, another method is to prepend the number of digits (fixed-width, zero-padded) to each string of digits. For example, if you won't have more than 99 digits in a row, then for "Super Blast 10 Ultra" the sort key would be "Super Blast 0210 Ultra".

提交回复
热议问题