Usually when I use "titles.where(status: 1).order("name")
", it correctly orders titles like:
"1. title one"
"2. title two"
but when titles like:
"1.1. Random title"
this puts it at the end of the ordering when it should go after "1. title one" and before "2. title two". What could I do there?
CodePudding user response:
This should do it:
titles.where(status: 1).sort_by{|title| title.name.split(" ").first.to_f}
Essentially it is creating a custom sort by grabbing that first number and converting it to a float to sort by
CodePudding user response:
It seems that your code already worked as intended. For example, if you run this code, it will give the result as in the photo:
titles = Title.where(status: 1).order("name").map(&:name)
puts titles.join("\n")