sort-last-name.rb
# Sort an array of names by last name using bubble sort
Sort an array of names by last name using bubble sort
Sort an array of names by last name using bubble sort
def sort_by_last_name(array)
  result = []
  array.each do |name|
    result << name.split(" ")
  end
  no_swap = false
  until no_swap
    no_swap = true
    result.each_with_index do |name, index|
      if result[index+1] && name[1] > result[index+1][1]
        result[index], result[index+1] = result[index+1], result[index]
        no_swap = false
      end
    end
  end
  result.map {|pair| pair.join(" ")}
end
def sort_by_last_name ( array ) result = [ ] array . each do | name | result << name . split ( " " ) end no_swap = false until no_swap no_swap = true result . each_with_index do | name , index | if result [ index + 1 ] && name [ 1 ] > result [ index + 1 ] [ 1 ] result [ index ] , result [ index + 1 ] = result [ index + 1 ] , result [ index ] no_swap = false end end end result . map { | pair | pair . join ( " " ) } end
Sort an array of names by last name using bubble sort
Sort an array of names by last name using bubble sort
array