简体   繁体   中英

find order by closest to number

How can I arrange order in Model.where(...).order() to sort the items based on proximity of a number field to assign number?

So, when I request to order for 4, (1...6) would be something like this: [4,5,3,6,2,1]

Sounds like you want the absolute value of the difference between a given number and the field value. For example:

User.order("abs(4 - <your-field-name-here>)")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM