I want sort an array of String by alphabet but force one to the front of the array
p ['Home', 'Contact', 'Profile', 'Jobs', 'Privacy'].sort { |x, y|
if x == 'Privacy'
-1
else
x <=> y
end
}
Gives me ["Contact", "Home", "Jobs", "Privacy", "Profolio"]
instead of ["Privacy", "Contact", "Home", "Jobs", "Profolio"]
Eventually I want to sort an array of objects by name attribute
class Page
include Comparable
def <=> (other)
if self.name == 'Jobs'
-1
else
self.name <=> other.name
end
end
end
[Page.new('Contact'), Page.new('Home'), Page.new('Jobs')...].sort
However overwriting <=> in the Comparable class doesn't work as well