comparer & sorting

This commit is contained in:
Inna Pruidze 2024-06-16 23:08:52 +04:00
parent 9c904ea947
commit 7fc6a50c1b
5 changed files with 19 additions and 17 deletions

View File

@ -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<DrawningBase?> comparer) =>

View File

@ -139,7 +139,7 @@ public class ArrayGenObj<T> : ICollectionGenObj<T>
}
}
public void CollectionSort(IComparer<T> comparer)
void ICollectionGenObj<T>.CollectionSort(IComparer<T?> comparer)
{
Array.Sort(_collection, comparer);
}

View File

@ -112,7 +112,7 @@ public class ListGenObj<T> : ICollectionGenObj<T>
}
}
public void CollectionSort(IComparer<T> comparer)
void ICollectionGenObj<T>.CollectionSort(IComparer<T?> comparer)
{
_collection.Sort(comparer);
}

View File

@ -9,17 +9,18 @@ public class DrawningShipCompare : IComparer<DrawningBase?>
{
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;

View File

@ -11,28 +11,30 @@ public class DrawiningShipEqutables : IEqualityComparer<DrawningBase?>
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)