This is part of a program that works out the logistics for a company that delivers stuff using trucks. There are multiple trucks and each truck gets a route, each route contains loads. I get the loads from a SQL query, each row of the recordset specifies to which route this load (row contents) belongs. If two loads belong to the same route, they should both go on the same truck, so the routes have to be joined together. However each load has different amounts of pallets and different amounts of stops. These pallets and stops have to be added together.
Below is the code I am currently using, can any of you guys think of a way to improve the algorithm or know of a better algorithm altogether?
The current one works well enough if the query returns a decent amount of Loads, but I am worried about how it will perform if there are many Loads.
STLoad is structure
Route is string
Approx_Pallets is string
Stops is string
Gross is string
end
arrSTLoads is array of STLoad
// Records returned by a SQL query have been read into this ^ array of structure
Done is boolean = False
WHILE Done = False
FOR i=1 TO ArrayCount(arrSTLoads)
FOR j=1 TO ArrayCount(arrSTLoads)
IF i<>j THEN // Prevents adding a load to itself
IF arrSTLoads[i].Route = arrSTLoads[j].Route THEN // If both Loads are on the same route, they should be joined together
arrSTLoads[i].nApprox_Pallets = Val(arrSTLoads[i].Approx_Pallets) + Val(arrSTLoads[j].Approx_Pallets) // Pallets of both Routes added together
arrSTLoads[i].nStop = Val(arrSTLoads[i].Stops) + Val(arrSTLoads[j].Stops) // Amount of Stops the two Loads require added together
ArrayDelete(arrSTLoads,j) // Keep the first Load and remove the second from the array of Structure.
GOTO Slip // Array length and iterator location are no longer correct, break out of both for loops
END
END
END
END
Slip:
Done = True // Used to determine wether there are any Loads left that still need to be joined.
FOR i=1 TO ArrayCount(arrSTLoads)
FOR j=1 TO ArrayCount(arrSTLoads)
IF i<>j THEN
IF arrSTLoads[i].Route = arrSTLoads[j].Route THEN
Done = False // Loads that have to be joined found, while loop has to go for another round
GOTO Slip2 // Loads have been found that still have to be joined, break out of both for loops
END
END
END
END
Slip2:
END