Insert a value into a numerically-ordered array in the correct order

def insert_value_in_order(array_of_values, new_value, update_array = nil)
	value_index = array_of_values.index(new_value)
	index = 0
	if !value_index && array_of_values.last.to_i > new_value
		i = 0
		while array_of_values[i].to_i < new_value do
			i += 1
		end
		index = i
	else
		index = value_index ? value_index : -1
	end
	return update_array == true || update_array == 1 ? array_of_values.insert(index, new_value) : index
end

source

Comments are closed.