PostgreSQL ORDER BY issue - natural sort

前端 未结 7 1291
日久生厌
日久生厌 2020-12-01 16:38

I\'ve got a Postgres ORDER BY issue with the following table:

em_code  name
EM001    AAA
EM999    BBB
EM1000   CCC

To insert a

7条回答
  •  眼角桃花
    2020-12-01 16:51

    This always comes up in questions and in my own development and I finally tired of tricky ways of doing this. I finally broke down and implemented it as a PostgreSQL extension:

    https://github.com/Bjond/pg_natural_sort_order

    It's free to use, MIT license.

    Basically it just normalizes the numerics (zero pre-pending numerics) within strings such that you can create an index column for full-speed sorting au naturel. The readme explains.

    The advantage is you can have a trigger do the work and not your application code. It will be calculated at machine-speed on the PostgreSQL server and migrations adding columns become simple and fast.

提交回复
热议问题