I am trying a pseudo implementation based on the limited information you have given.
Let's have Street
and Intersection
classes and some Database
that can be queried for a list of objects with where filters (think of it as some SQL wrapper).
Now, we need a sorting algorithm for the intersections. The problem here is, that we cannot know the physical order based on the given information. Roads may go in curves, tunnels and what not.
So the next best thing we can do is guessing the order by evaluating the distance. Note that this will only work in a Manhattan type map layout without curves.
We will arbitrarily assume the first intersection would be the one with the lowest coordinate value.
From there, we search all our streets remaining intersections for the closest to the previous.
Again, please be aware, that this algorithm is a guess based on the given information. However, the general implementation pattern should still be usable for more sophisticated algorithms or the incorporation of additional information.
Please see my implementation in pseudo-code:
Database {
allIntersections {...}
allStreets {...}
}
Street {
id: String
name: String
intersections() -> [Intersection] {
return Database.allIntersections.where(street1 == self || street2 == self)
}
lowestIntersection() -> Intersection {
lowestIntersection: Intersections().first
for intersection in Intersections() {
currentDistance = intersection.distanceTo(Coordinate(0,0))
if (currentDistance < lowestIntersection.distanceTo(Coordinate(0,0)) {
lowestIntersection = intersection
}
}
return lowestIntersection
}
// the sort algorithm based on proximity
sortedIntersections() -> [Intersection] {
sorted = [lowestIntersection()]
remaining = intersections() - sorted
while remaining.count > 0 {
closest = sorted.last.closestIntersection(remaining)
remaining.remove(closest)
sorted.add(closest)
}
return sorted
}
}
Coordinate {
lat: Float
long: Float
}
Intersection {
street1: Street
street2: Street
coordinate: Coordinate
distanceTo(other: Coordinate) -> Float {
return sqrt(pow2(coordinate.lat - other.lat) + pow2(coordinate.long - other.long))
}
closesIntersection(others: Intersection) -> Intersection {
closesIntersection: others.first
for intersection in others {
currentDistance = intersection.distanceTo(self.coordinate)
if (currentDistance < closesIntersection.distanceTo(self.coordinate) {
closesIntersection = intersection
}
}
return closesIntersection
}
}
I hope this helps. For a more detailed answer, please provide more information like example data.