I am writing a class that will sort an array of airport codes in a way that would simulate an aircrafts flight path between multiple cities.
For example: Given an array [['LAX', 'BWI'], ['BOS', 'SEA'], ['HNL', 'LAX'], ['SEA', 'HNL']]
where the first index of each sub array is the
departing airport and the second is the destination airport, the sort_flight_path
method would return [['BOS', 'SEA'], ['SEA', 'HNL'], ['HNL', 'LAX'], ['LAX', 'BWI']]
.
I have a method that finds the start point as an index value, and I have the following method that returns a new sorted array.
def sort_flight_path
flight_path = [] << @list[@start_point]
start = @start_point
until flight_path.size == @list.size
@list.each do |i|
if @list[start][1] == i[0]
flight_path << i
start = @list.index(i)
end
end
end
flight_path
end
As I am becoming more comfortable with Ruby, I want to find a more "Ruby like" way to accomplish this.