Last active
August 7, 2025 13:24
-
-
Save nickbayley/d0cd8baae014836a3d1c0f1606fd0217 to your computer and use it in GitHub Desktop.
Ordered Types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void main() { | |
| var rootTypes = ["ABH", "ASH", "POH"]; | |
| //var rootTypes = ["ABH", "POH", "ASH"]; | |
| //var rootTypes = ["POH", "ASH", "ABH"]; | |
| //var rootTypes = ["POH", "ABH", "ASH"]; | |
| //var rootTypes = ["ASH", "POH", "ABH"]; | |
| //var rootTypes = ["ASH", "ABH", "POH"]; | |
| var orderedRootTypes = rootTypes; | |
| final precedingTelegrams = { "ABH": ["POH", "ASH"], "ASH": ["POH"] }; | |
| print('Start order: $rootTypes'); | |
| for (var type in rootTypes) { | |
| print('Iterating through root types: $type'); | |
| var currentIndex = orderedRootTypes.indexOf(type); | |
| orderedRootTypes.removeAt(currentIndex); | |
| var maxIndex = precedingTelegrams[type]?.fold<int>(-1, (max, precedingTelegram) { | |
| var index = orderedRootTypes.indexOf(precedingTelegram); | |
| return index > max ? index : max; | |
| }) ?? -1; | |
| if (maxIndex >= currentIndex) { | |
| orderedRootTypes.insert(maxIndex + 1, type); | |
| print('New order: $orderedRootTypes'); | |
| continue; | |
| } | |
| orderedRootTypes.insert(currentIndex, type); | |
| print('New order: $orderedRootTypes'); | |
| } | |
| print('Final order: $orderedRootTypes'); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment