Skip to content

Instantly share code, notes, and snippets.

@yangsu
Last active December 11, 2015 20:08
Show Gist options
  • Save yangsu/4652883 to your computer and use it in GitHub Desktop.
Save yangsu/4652883 to your computer and use it in GitHub Desktop.
Ruby Sorting Example
class SizeMatters
include Comparable
attr :str
def <=>(anOther)
str.size <=> anOther.str.size
end
def initialize(str)
@str = str
end
def inspect
@str
end
end
[
SizeMatters.new("S"),
SizeMatters.new("SSSS"),
SizeMatters.new("SSSSS"),
SizeMatters.new("SS"),
SizeMatters.new("SSS")
].sort
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment