Skip to content

Instantly share code, notes, and snippets.

@leahgarrett
Created March 26, 2019 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leahgarrett/142091a92f63f2162c1c15684101138f to your computer and use it in GitHub Desktop.
Save leahgarrett/142091a92f63f2162c1c15684101138f to your computer and use it in GitHub Desktop.
select, sort and find

select, sort and find


Demo code will use the person class from yesterday
class Person
  attr_reader :name, :job_title, :phone, :salary

  @employee_number = 0
  def initialize(name, job_title, phone, salary)
    @name = name
    @job_title = job_title
    @phone = phone
    @salary = salary.to_i
    @number = Person.increase_number
  end

  def self.increase_number
    @employee_number += 1
  end

  def description
    "##{@number} #{@name} is a #{@job_title} and has salary #{@salary}"
  end
end

people = [Person.new('Kerry MacFarlane', 'developer', '555-1234', '60000'),
          Person.new('Terry Grayson', 'manager', '555-3456', '90000'),
          Person.new('Sam Ferguson', 'developer', '555-4321', '65000'),
          Person.new('Jossef Goldberg', 'designer', '555-2222', '65000'),
          Person.new('Doris Hartwig', 'engineer', '555-6767', '75000'),
          Person.new('Steven Selikoff', 'developer', '555-2424', '65000'),
          Person.new('Zheng Mu', 'developer', '555-5885', '65000'),
          Person.new('Greg Komosinski', 'analyst', '555-9090', '55000')
          ]

select

array = Array.new(10) { rand(1...100) }
print array
puts
selected_numbers = array.select do |number|
    number > 50
  end
print selected_numbers
puts

Selecting objects based on instance variable values

selected_people = people.select do |person|
  person.salary > 65_000
end
puts 'selected people'
selected_people.each { |p| puts p.description }

sort

Sort example with numbers

array = Array.new(10) { rand(1...100) }

print array
puts
print array.sort
puts

Sort example with strings

array = ['4', '23', '45', '2', '19']

print array
puts
print array.sort 
puts

Can resolve the string sort issue using a custom sort

array = ['4', '23', '45', '2', '19']

print array
puts
print array.sort { |x, y| x.to_i <=> y.to_i }
puts

Sorting objects

puts 'before sort'

people.each { |p| puts p.description }

sorted_people = people.sort { |x, y| x.name <=> y.name }

puts 'after sort'

sorted_people.each { |p| puts p.description }

find

  • find first instance of
  • returns one value
array = [23, 56, 22, 17, 85]

value = array.find do |item|
    item > 23
end

puts "value found #{value}"

Can also be written as

value = [23, 56, 22, 17, 85].find { |item| item > 23 }

puts "value found #{value}"

Using the Person class from earlier

result = people.find do |person|
    person.salary > 65000
end

puts result.description

array = Array.new(10) { rand(1...100) }
puts 'starting array of random numbers'
print array
puts
selected_numbers = array.select do |number|
number > 50
end
puts 'selected numbers'
print selected_numbers
puts
print array
puts
print array.sort
puts
array = ['4', '23', '45', '2', '19']
puts 'before sort'
print array
puts
puts 'default sort'
print array.sort
puts
puts 'custom sort'
print array.sort { |x, y| x.to_i <=> y.to_i }
puts
# change the x and y to see what happens
# class to demo ruby features
class Person
attr_reader :name, :job_title, :phone, :salary
@employee_number = 0
def initialize(name, job_title, phone, salary)
@name = name
@job_title = job_title
@phone = phone
@salary = salary.to_i
@number = Person.increase_number
end
def self.increase_number
@employee_number += 1
end
def description
"##{@number} #{@name} is a #{@job_title} and has salary #{@salary}"
end
end
require_relative 'person'
people = [
Person.new('Kerry MacFarlane', 'developer', '555-1234', '60000'),
Person.new('Terry Grayson', 'manager', '555-3456', '90000'),
Person.new('Sam Ferguson', 'developer', '555-4321', '65000'),
Person.new('Jossef Goldberg', 'designer', '555-2222', '65000'),
Person.new('Doris Hartwig', 'engineer', '555-6767', '75000'),
Person.new('Steven Selikoff', 'developer', '555-2424', '65000'),
Person.new('Zheng Mu', 'developer', '555-5885', '65000'),
Person.new('Greg Komosinski', 'analyst', '555-9090', '55000')
]
selected_people = people.select do |person|
person.salary > 65_000
end
puts 'selected people'
selected_people.each { |p| puts p.description }
puts 'before sort'
people.each { |p| puts p.description }
sorted_people = people.sort { |x, y| y.name <=> x.name }
puts 'after sort'
sorted_people.each { |p| puts p.description }
puts 'find person'
result = people.find do |person|
person.salary > 65_000
end
puts result.description
# Refactor the above code to create helper methods to do the following
# - display an array of person objects
# - select people in an array of person objects based on a minimum
# and maximum salary
# - find a person of a particular salary value in an array of person objects
# - sort poeple by name
# - sort people by salary
# - get the person with the highest salary
# - get the person with the lowest salary
# Write code to call each of the above methods.
# The code should a couple examples of the select and find methods,
# including when there are no resultsd.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment