1

我有一个有很多标题的 Post 模型。但也有一个主要标题被标记。标题包含文本和数字。

我尝试按如下方式对帖子列表进行排序:

joins(:titles).where(main: true).order('titles.value ASC')

结果:

abc 1
abc 12
abc 2
bcd 1
bcd 15
bcd 2

仅按文本排序。请告诉我,如何按文本(按字母顺序)以及按数字排序?

4

1 回答 1

3

我尝试了以下代码:

joins(:titles).where(main: true).order("left(lower(titles.value), 1), substring(titles.value, '\\d+')::int NULLS FIRST, titles.value")

它按应有的方式排序。

于 2019-08-20T11:15:50.193 回答