I have an array of strings:
arr = ["2.1", "2.2", "2.2.1", "2.2.10", "2.2.2", "2.10"]
If I use arr.sort, the result is:
["2.1", "2.10", "2.2", "2.2.1", "2.2.10", "2.2.2"]
I need:
["2.1", "2.2", "2.2.1", "2.2.2", "2.2.10", "2.10"]
How can I sort it correctly?
Question 2:
How can I sort “alpha” or “beta”-type characters like “2.1a” or “0.9b”
Question 3:
If I have an array of active records with field type of string, which contains number like that, is it real to set in model default_scope :order => ... with sort_by{|a| a.split('.').map &:to_i }
Or
Create my method in model something like
def my_sort
#sorting
end
For this:
Block.all.my_sort
Which will:
spliteach of the strings into components.map &:to_i).