Skip to content

Instantly share code, notes, and snippets.

@nickbayley
Last active August 7, 2025 13:24
Show Gist options
  • Save nickbayley/d0cd8baae014836a3d1c0f1606fd0217 to your computer and use it in GitHub Desktop.
Save nickbayley/d0cd8baae014836a3d1c0f1606fd0217 to your computer and use it in GitHub Desktop.
Ordered Types
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