Advertisement
wtmhahagd

array doodad

Jun 13th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.68 KB | None | 0 0
  1. class Thing
  2.   def initialize(id)
  3.     @id = id
  4.   end
  5.  
  6.   def getId
  7.     return @id
  8.   end
  9. end
  10.  
  11. def arrayContainsId(array, id)
  12.   array.each do |thing|
  13.     return true if thing.getId == id
  14.   end
  15.   return false
  16. end
  17.  
  18. things = []
  19. 10.times do |count|
  20.   things[count] = Thing.new(count + 1)
  21. end
  22.  
  23. newOrder = [69, 2, 3, 5, 25, 4, 8, 9, 6, 420]
  24.  
  25. # remove every Thing in things[] that is not in the new newOrder
  26. things.delete_if{ |thing| !newOrder.include? thing.getId }
  27.  
  28. # add new things
  29. newOrder.each do |id|
  30.   things.push Thing.new(id) if !arrayContainsId(things, id)
  31. end
  32.  
  33. # reorder
  34. things.sort_by!{|x| newOrder.index x.getId}
  35.  
  36. things.each do |things|
  37.   puts things.getId
  38. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement