Last active
June 13, 2019 07:07
-
-
Save alexd1971/156861752c6e34d47060825dfe538c96 to your computer and use it in GitHub Desktop.
This file contains 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
/// Выполняет сравнение нарядов по критерию связанности | |
int compareDetachments (Detachment a, Detachment b) { | |
final finishAtComparison = a.finishAt.compareTo(b.finishAt); | |
if(finishAtComparison != 0) return finishAtComparison; | |
return a.order.id.compareTo(b.order.id); | |
} | |
typedef bool TestRelated(Detachment detachment); | |
/// Возвращает функцию типа [TestRelated], которая проверяет, | |
/// является ли наряд связанным с указанным нарядом [oneOfRelated] | |
TestRelated isRelatedTo(Detachment oneOfRelated) => | |
(Detachment detachment) => compareDetachments(oneOfRelated, detachment) == 0; | |
/// Группирует связанные наряды | |
/// | |
/// Для правильной группировки наряды должны быть отсортированы так, | |
/// чтобы связанные наряды шли один за другим: | |
/// | |
/// ``` | |
/// [relatedA1, relatedA2,..., relatedAN, relatedB1, relatedB2,...] | |
List<List<Detachment>> groupRelated(Iterable<Detachment> detachments) { | |
if(detachments.isEmpty) return []; | |
TestRelated isRelated = isRelatedTo(detachments.first); | |
return [detachments.takeWhile(isRelated).toList()]..addAll(groupRelated(detachments.skipWhile(isRelated))); | |
} | |
void main() { | |
print (groupRelated(detachments..sort(compareDetachments))); | |
} | |
// Все, что ниже, нужно только для проверки работоспособности | |
class Detachment { | |
final DateTime finishAt; | |
final Order order; | |
Detachment(this.order, this.finishAt); | |
@override | |
String toString() => '(${order.id} - $finishAt)'; | |
} | |
class Order { | |
final String id; | |
Order(this.id); | |
} | |
final detachments = <Detachment>[ | |
Detachment(Order('1'), DateTime(2019, 06, 12)), | |
Detachment(Order('2'), DateTime(2019, 06, 13)), | |
Detachment(Order('1'), DateTime(2019, 06, 12)), | |
Detachment(Order('2'), DateTime(2019, 06, 13)), | |
Detachment(Order('1'), DateTime(2019, 06, 12)), | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment