Here is my problem: I have an array of string which contains data like that:
array = ["{109}{08} OK",
"{98} Thx",
"{108}{0.8}{908} aa",
"{8}{51} lorem ipsum"]
I would like to sort this array scanning "data inside": here the integers in brace. So, the final array should be like that :
array.custom_sort! => ["{8}{51} lorem ipsum",
"{98} Thx",
"{108}{0.8}{908} aa",
"{109}{08} OK"]
Is there a nice solution to do it in Ruby? Or should I recreate a new array which inserts each parsed elements?
EDIT:
I failed to mention the sort priorities: First, the sorting is based on the number in braces, up to 3 groups, but cannot be absent.
["{5}something",
"{61}{64}could",
"{}be", #raise an error or ignore it
"{54}{31.24}{0.2}write",
"{11}{21}{87}{65}here", #raise an error or ignore it
"[]or", #raise an error or ignore it
"{31}not"]
If the first numbers are equal, then the second ones should be compared. Some examples:
"{15}" < "{151}" < "{151}{32}" < "{152}"
"{1}" < "{012}" < "{12}{-1}{0}" < "{12.0}{0.2}"
"{5}" < "{5}{0}" < "{5}{0}{1}"
But if every numbers are equals, then the string is compares. The only character which make a problem is the space, which must be after every other "visible" characters. Examples:
"{1}a" < "{1}aa" < "{1} a" < "{1} a"
"{1}" < "{1}a " < "{1}a " < "{1}a a"
"{1}a" < "{1}ba" < "{1}b "
I can make it doing somethign like this in a custom class:
class CustomArray
attr_accessor :one
attr_accessor :two
attr_accessor :three
attr_accessor :text
def <=>(other)
if self.one.to_f < other.one.to_f
return -1
elsif self.one.to_f > other.one.to_f
return 1
elsif self.two.nil?
if other.two.nil?
min = [self.text, other.text].min
i = 0
until i == min
if self.text[i].chr == ' ' #.chr is for compatibility with Ruby 1.8.x
if other.text[i].chr != ' '
return 1
end
else
if other.text[i].chr == ' '
return -1
#...
self.text <=> other.text
end
end
It works fine, but I am very frustrated coding in Ruby like I code in C++ project. That is why I would like to know how to use a "custom sort in a foreach method" with a more complexe sort way (require parsing, scanning, regexp) than a naive one based on an attribute of the content.