From 7fc6a50c1b404b44a9988c256568a3e244e74d62 Mon Sep 17 00:00:00 2001 From: DelphyAlmond Date: Sun, 16 Jun 2024 23:08:52 +0400 Subject: [PATCH] comparer & sorting --- .../CollectionGenericObj/AbstractCompany.cs | 5 ++-- .../CollectionGenericObj/ArrayGenObj.cs | 2 +- .../CollectionGenericObj/ListGenObj.cs | 2 +- .../DrawningSamples/DrawningShipCompare.cs | 3 ++- .../DrawningSamples/DrawningShipEqutables.cs | 24 ++++++++++--------- 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/ProjectCruiser/CollectionGenericObj/AbstractCompany.cs b/ProjectCruiser/CollectionGenericObj/AbstractCompany.cs index fdfd45f..7af6814 100644 --- a/ProjectCruiser/CollectionGenericObj/AbstractCompany.cs +++ b/ProjectCruiser/CollectionGenericObj/AbstractCompany.cs @@ -31,12 +31,11 @@ public abstract class AbstractCompany // Перегрузка оператора сложения для класса public static int operator +(AbstractCompany company, - DrawningBase trasport) => - company._collection.Insert(trasport, new DrawiningShipEqutables()); + DrawningBase transport) => company._collection.Insert(transport, new DrawiningShipEqutables()); // Перегрузка оператора удаления для класса public static DrawningBase operator -(AbstractCompany company, - int pos) => company._collection.Remove(pos); + int pos) => company._collection?.Remove(pos); // Сортировка ----------------------------------------------------------- [!] public void Sort(IComparer comparer) => diff --git a/ProjectCruiser/CollectionGenericObj/ArrayGenObj.cs b/ProjectCruiser/CollectionGenericObj/ArrayGenObj.cs index 0bfe319..96af3bb 100644 --- a/ProjectCruiser/CollectionGenericObj/ArrayGenObj.cs +++ b/ProjectCruiser/CollectionGenericObj/ArrayGenObj.cs @@ -139,7 +139,7 @@ public class ArrayGenObj : ICollectionGenObj } } - public void CollectionSort(IComparer comparer) + void ICollectionGenObj.CollectionSort(IComparer comparer) { Array.Sort(_collection, comparer); } diff --git a/ProjectCruiser/CollectionGenericObj/ListGenObj.cs b/ProjectCruiser/CollectionGenericObj/ListGenObj.cs index 9c1fb48..3551498 100644 --- a/ProjectCruiser/CollectionGenericObj/ListGenObj.cs +++ b/ProjectCruiser/CollectionGenericObj/ListGenObj.cs @@ -112,7 +112,7 @@ public class ListGenObj : ICollectionGenObj } } - public void CollectionSort(IComparer comparer) + void ICollectionGenObj.CollectionSort(IComparer comparer) { _collection.Sort(comparer); } diff --git a/ProjectCruiser/DrawningSamples/DrawningShipCompare.cs b/ProjectCruiser/DrawningSamples/DrawningShipCompare.cs index 3792ba9..cb7a8a4 100644 --- a/ProjectCruiser/DrawningSamples/DrawningShipCompare.cs +++ b/ProjectCruiser/DrawningSamples/DrawningShipCompare.cs @@ -9,17 +9,18 @@ public class DrawningShipCompare : IComparer { return -1; } + if (y == null || y.EntityTransport == null) { return 1; } + if (x.GetType().Name != y.GetType().Name) { return x.GetType().Name.CompareTo(y.GetType().Name); } var speedCompare = x.EntityTransport.Speed.CompareTo(y.EntityTransport.Speed); - if (speedCompare != 0) { return speedCompare; diff --git a/ProjectCruiser/DrawningSamples/DrawningShipEqutables.cs b/ProjectCruiser/DrawningSamples/DrawningShipEqutables.cs index fb19dbd..6a97f66 100644 --- a/ProjectCruiser/DrawningSamples/DrawningShipEqutables.cs +++ b/ProjectCruiser/DrawningSamples/DrawningShipEqutables.cs @@ -11,28 +11,30 @@ public class DrawiningShipEqutables : IEqualityComparer if (x == null || x.EntityTransport == null) return false; if (y == null || y.EntityTransport == null) return false; if (x.GetType().Name != y.GetType().Name) return false; + if (x.EntityTransport.Speed != y.EntityTransport.Speed) return false; if (x.EntityTransport.Weight != y.EntityTransport.Weight) return false; if (x.EntityTransport.MainColor != y.EntityTransport.MainColor) return false; if (x is DrawningCruiser && y is DrawningCruiser) { - /* - public Color AdditionalColor { get; private set; } // доп. цвет + /* public Color AdditionalColor { get; private set; } // доп. цвет // признаки (наличия) public bool HelicopterPads { get; private set; } // вертолетная площадка - public bool Hangars { get; private set; } // ангар - */ - if (((EntityCruiser)x.EntityTransport).AdditionalColor - != ((EntityCruiser)y.EntityTransport).AdditionalColor) return false; + public bool Hangars { get; private set; } // ангар */ - if (((EntityCruiser)x.EntityTransport).HelicopterPads - != ((EntityCruiser)y.EntityTransport).HelicopterPads) return false; + EntityCruiser EntityX = (EntityCruiser)x.EntityTransport; + EntityCruiser EntityY = (EntityCruiser)y.EntityTransport; - if (((EntityCruiser)x.EntityTransport).Hangars - != ((EntityCruiser)y.EntityTransport).Hangars) return false; + if (EntityX.AdditionalColor != EntityY.AdditionalColor) + return false; + + if (EntityX.Hangars != EntityY.Hangars) + return false; + + if (EntityX.HelicopterPads != EntityY.HelicopterPads) + return false; } - return true; } public int GetHashCode([DisallowNull] DrawningBase obj)