Skip to content

Instantly share code, notes, and snippets.

@cwgem
Created July 28, 2011 17:09
Show Gist options
  • Select an option

  • Save cwgem/1111978 to your computer and use it in GitHub Desktop.

Select an option

Save cwgem/1111978 to your computer and use it in GitHub Desktop.
カスタムソートのフールソース
class Employee
attr_accessor :first_name
attr_accessor :last_name
attr_accessor :monthly_sales
include Comparable
# 月間売上高で社員をソート
def <=>(other_employee)
other_employee.monthly_sales <=> @monthly_sales
end
def to_s
"#{@first_name} #{@last_name}: #{@monthly_sales}"
end
end
# ランダム数 最小-最大
def range_rand(min,max)
min + rand(max-min)
end
# ランダム名前
$names = File.new('names.txt', 'r').readlines.map {|line| line.chomp}
def rand_name
$names[rand($names.length)]
end
# ランダムデータで社員3人作ります
employees = Array.new(3) { |index|
employee = Employee.new
employee.first_name = rand_name
employee.last_name = rand_name
employee.monthly_sales = range_rand(1000,10000)
employee
}
# ソート前
puts employees.inspect
# ソート後
puts employees.sort.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment