edit code so that sort the bus timing based on the  departure time of departure station
class Api::V1::Schedules2Controller < ApplicationController
def index
departure_station = params[:departure]
destination_station = params[:destination]

if departure_station.present? && destination_station.present?
# Start by fetching schedules that include both departure and destination stations
schedules = Schedule.joins(:stations, route: :bus_schedule)
.where(stations: { name: [departure_station, destination_station] })
.distinct
.includes(:stations, route: :bus_schedule)

# Iterate through schedules to filter valid ones and prepare the response data
vehicle_trips = schedules.each_with_object({}) do |schedule, acc|
# Extract station names to check the order without triggering extra queries
station_names = schedule.stations.map(&:name)

# Ensure that the destination comes after the departure
if station_names.include?(departure_station) &&
station_names.include?(destination_station) &&
station_names.index(departure_station) < station_names.index(destination_station)

vehicle_number = schedule.route.bus_schedule.vehicle_number
trip_data = {
trip: schedule.trip,
stations: schedule.stations.map do |station|
{
station: station.name,
arrivalTime: station.arrival_time,
departureTime: station.departure_time
}
end
}

# Accumulate trips for each vehicle_number
acc[vehicle_number] ||= []
acc[vehicle_number] << trip_data
end
end

# Render the final JSON response
render json: vehicle_trips.map { |vehicle_number, trips|
{
vehicle_number: vehicle_number,
trips: trips
}
}, status: :ok
else
render json: { error: ‘Departure and destination stations are required.’ }, status: :unprocessable_entity
end
end
end

sample output json
[{“vehicle_number”:“KL 41 7790”,“trips”:[{“trip”:3,“stations”:[{“station”:“ALUVA”,“arrivalTime”:“08:55 am”,“departureTime”:“08:55 am”},{“station”:“FORTKOCHI BUS TERMINAL”,“arrivalTime”:“10:31 am”,“departureTime”:“10:31 am”}]},{“trip”:5,“stations”:[{“station”:“ALUVA”,“arrivalTime”:“01:16 pm”,“departureTime”:“01:16 pm”},{“station”:“FORTKOCHI BUS TERMINAL”,“arrivalTime”:“02:52 pm”,“departureTime”:“02:52 pm”}]},{“trip”:7,“stations”:[{“station”:“ALUVA”,“arrivalTime”:“04:38 pm”,“departureTime”:“04:38 pm”},{“station”:“FORTKOCHI BUS TERMINAL”,“arrivalTime”:“06:14 pm”,“departureTime”:“06:14 pm”}]}]},{“vehicle_number”:“KL 41 G 0939”,“trips”:[{“trip”:1,“stations”:[{“station”:“ALUVA”,“arrivalTime”:“06:34 am”,“departureTime”:“06:34 am”},{“station”:“FORTKOCHI BUS TERMINAL”,“arrivalTime”:“08:44 am”,“departureTime”:“08:44 am”}]},{“trip”:3,“stations”:[{“station”:“ALUVA”,“arrivalTime”:“10:43 am”,“departureTime”:“10:43 am”},{“station”:“FORTKOCHI BUS TERMINAL”,“arrivalTime”:“12:20 pm”,“departureTime”:“12:20 pm”}]},{“trip”:5,“stations”:[{“station”:“ALUVA”,“arrivalTime”:“02:49 pm”,“departureTime”:“02:49 pm”},{“station”:“FORTKOCHI BUS TERMINAL”,“arrivalTime”:“04:26 pm”,“departureTime”:“04:26 pm”}]},{“trip”:7,“stations”:[{“station”:“ALUVA”,“arrivalTime”:“06:43 pm”,“departureTime”:“06:43 pm”},{“station”:“FORTKOCHI BUS TERMINAL”,“arrivalTime”:“08:19 pm”,“departureTime”:“08:19 pm”}]}]},{“vehicle_number”:“KL 07 AQ 6989”,“trips”:[{“trip”:5,“stations”:[{“station”:“ALUVA”,“arrivalTime”:“12:37 pm”,“departureTime”:“12:37 pm”},{“station”:“FORTKOCHI BUS TERMINAL”,“arrivalTime”:“02:13 pm”,“departureTime”:“02:13 pm”}]}]},{“vehicle_number”:“KL 07 AP 7651”,“trips”:[{“trip”:1,“stations”:[{“station”:“ALUVA”,“arrivalTime”:“07:35 am”,“departureTime”:“07:35 am”},{“station”:“FORTKOCHI BUS TERMINAL”,“arrivalTime”:“09:11 am”,“departureTime”:“09:11 am”}]},{“trip”:3,“stations”:[{“station”:“ALUVA”,“arrivalTime”:“11:08 am”,“departureTime”:“11:08 am”},{“station”:“FORTKOCHI BUS TERMINAL”,“arrivalTime”:“12:44 pm”,“departureTime”:“12:44 pm”}]},{“trip”:5,“stations”:[{“station”:“ALUVA”,“arrivalTime”:“03:27 pm”,“departureTime”:“03:27 pm”},{“station”:“FORTKOCHI BUS TERMINAL”,“arrivalTime”:“05:03 pm”,“departureTime”:“05:03 pm”}]},{“trip”:7,“stations”:[{“station”:“ALUVA”,“arrivalTime”:“07:16 pm”,“departureTime”:“07:16 pm”},{“station”:“FORTKOCHI BUS TERMINAL”,“arrivalTime”:“08:52 pm”,“departureTime”:“08:52 pm”}]}]},{“vehicle_number”:“KL 41 R 1343”,“trips”:[{“trip”:5,“stations”:[{“station”:“ALUVA”,“arrivalTime”:“12:40 pm”,“departureTime”:“12:40 pm”},{“station”:“FORTKOCHI BUS TERMINAL”,“arrivalTime”:“02:16 pm”,“departureTime”:“02:16 pm”}]},{“trip”:7,“stations”:[{“station”:“ALUVA”,“arrivalTime”:“04:05 pm”,“departureTime”:“04:05 pm”},{“station”:“FORTKOCHI BUS TERMINAL”,“arrivalTime”:“05:41 pm”,“departureTime”:“05:41 pm”}]}]}]