So I'm going through these practice problems and maybe there's a better way to do this but this is what I got. I am severely confused why this only seems to work sometimes...

```
def nearest_larger(arr, idx)
bigs = []

arr.each do |num|
    if num > arr[idx]
      bigs << num
    end
end

sorted = bigs.sort
    if sorted.length > 1
      sorted = sorted[0]
    end

return arr.index(sorted)
end

puts nearest_larger([2, 3, 4, 8], 2) #DOES NOT WORK
puts nearest_larger([2, 3, 4, 8], 2) #DOES WORK
puts nearest_larger([8, 8, 4, 2], 2) #ALSO WORKS... why?!!!

```