intprog_hackery/sorting_algos.rb

93 lines
2.6 KiB
Ruby

# TODO: Make variadic, such that we only need one method
def compare_to_two_vars(first, second)
if first.type == "String"
if_statement = "
if (!this.#{first.variable}.equals(other.#{first.variable}) {
return this.#{first.variable}.compareTo(other.#{first.variable});
}
"
else
if first.comparator == ">"
return_string = "return this.#{first.variable} - other.#{first.variable};"
else
return_string = "return other.#{first.variable} - this.#{first.variable};"
end
if_statement = "
if (this.#{first_variable} != other.#{first.variable} {
#{return_string}
}
"
end
if second.type == "String"
return_string = "return this.#{second.variable}.compareTo(other.#{second.variable});"
else
if second.comparator == ">"
return_string = "return this.#{second.variable} - other.#{second.variable};"
else
return_string = "return other.#{second.variable} - this.#{second.variable};"
end
end
"
#{if_statement}
#{return_string}
"
end
def compare_to_three_vars(first, second, third)
if first.type == "String"
if_statement = "
if (!this.#{first.variable}.equals(other.#{first.variable})) {
return this.#{first.variable}.compareTo(other.#{first.variable});
}
"
else
if first.comparator == ">"
return_string = "return this.#{first.variable} - other.#{first.variable};"
else
return_string = "return other.#{first.variable} - this.#{first.variable};"
end
if_statement = "
if (this.#{first.variable} != other.#{first.variable}) {
#{return_string}
}
"
end
if second.type == "String"
else_if_statement = "
else if (!this.#{second.variable}.equals(other.#{second.variable})) {
return this.#{second.variable}.compareTo(other.#{second.variable};
}
"
else
if second.comparator == ">"
return_string = "return this.#{second.variable} - other.#{second.variable};"
else
return_string = "return other.#{second.variable} - this.#{second.variable};"
end
else_if_statement = "
else if (this.#{second.variable} != other.#{second.variable}) {
#{return_string}
}
"
end
if third.type == "String"
return_string = "return this.#{third.variable}.compareTo(other.#{third.variable});"
else
if third.comparator == ">"
return_string = "return this.#{third.variable} - other.#{third.variable};"
else
return_string = "return other.#{third.variable} - this.#{third.variable};"
end
end
"
#{if_statement}
#{else_if_statement}
#{return_string}
"
end