How can I order a list of connections
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
A "list of connections" usually means a set of edges such as A -> B, B -> C, C -> D that you want to arrange into path order. That is not a normal sort by one field. It is a graph-ordering problem, and the solution depends on whether the connections form one simple chain, several chains, or a graph with branches and cycles.
Case 1: The Connections Form One Chain
If each connection has a single start and end, and the data represents one continuous path, you can rebuild the ordered list by finding the unique starting node and walking forward.
This works well when the structure is a simple path with one clear beginning.
Why a Normal Sort Is Not Enough
A regular sort() call compares items independently. It cannot express a rule such as "this pair must come immediately before the pair whose source equals my destination".
That is why the problem feels like sorting but is really about adjacency.
If you only sort lexicographically, you might get this:
That can still be wrong for path order even if it looks neat.
Case 2: Multiple Chains or Branches
If the connections are not a single chain, you need to think in graph terms. You may have:
- multiple disconnected paths
- a branching structure where one node points to several others
- a cycle with no natural start node
In that case, there is no single universal ordered list of edges unless you add more rules.
A useful first step is to compute indegree and outdegree to understand the structure.
This tells you whether you have one chain or several disconnected components.
Case 3: You Need Topological Order
If the connections represent prerequisites or dependencies rather than one physical chain, use a topological sort instead of path reconstruction.
That solves dependency ordering, not necessarily edge-by-edge chain reconstruction.
Common Pitfalls
- Treating connection ordering as a normal sort problem when it is actually about graph structure.
- Assuming there is one valid order even when the data contains branches or multiple disconnected paths.
- Ignoring cycles, which make simple "find the start and walk" logic fail.
- Building the forward map without checking whether a source appears more than once.
- Solving a dependency graph with path logic when a topological sort is what you really need.
Summary
- Ordering connections depends on the structure behind the data.
- For one simple chain, find the unique start node and walk through the mapping.
- For more complex data, inspect indegree, outdegree, and connected components first.
- Use topological sort when the connections represent dependencies rather than one path.
- A normal sort is usually the wrong tool unless the connections already contain a sortable sequence field.
Related reading
- How can I programmatically determine how to fit smaller boxes into a larger package?
- How can I reorder a list?
- How can I reverse a linked list?
- How can I sort a coordinate list for a rectangle counterclockwise?
- How can I sort a List alphabetically?
- How can I sort a stdmap first by value, then by key?
- How can I sort an STL map by value?
- How can I sort generic list DESC and ASC?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.