Второй шаг: сортировка объектов

This commit is contained in:
prodigygirl 2022-11-30 18:03:40 +04:00
parent e27ebecf0e
commit bd3d96264e
5 changed files with 142 additions and 0 deletions

View File

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ArmoredCar
{
internal class ArmoredCarCompareByColor : IComparer<IDrawningObject>
{
public int Compare(IDrawningObject x, IDrawningObject y)
{
if (x == null && y == null)
{
return 0;
}
if (x == null && y != null)
{
return 1;
}
if (x != null && y == null)
{
return -1;
}
var xCar = x as DrawningObjectArmCar;
var yCar = y as DrawningObjectArmCar;
if (xCar == null && yCar == null)
{
return 0;
}
if (xCar == null && yCar != null)
{
return 1;
}
if (xCar != null && yCar == null)
{
return -1;
}
string xCarColor = xCar.GetArmoredCar.ArmoredCar.BodyColor.Name;
string yCarColor = yCar.GetArmoredCar.ArmoredCar.BodyColor.Name;
if (xCarColor != yCarColor)
{
return xCarColor.CompareTo(yCarColor);
}
if (xCar.GetArmoredCar.ArmoredCar is EntityTank xTank && yCar.GetArmoredCar.ArmoredCar is EntityTank yTank)
{
string xCarDopColor = xTank.DopColor.Name;
string yCarDopColor = yTank.DopColor.Name;
if (xCarDopColor != yCarDopColor)
{
return xCarColor.CompareTo(yCarColor);
}
}
var speedCompare = xCar.GetArmoredCar.ArmoredCar.Speed.CompareTo(yCar.GetArmoredCar.ArmoredCar.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xCar.GetArmoredCar.ArmoredCar.Weight.CompareTo(yCar.GetArmoredCar.ArmoredCar.Weight);
}
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ArmoredCar
{
internal class ArmoredCarCompareByType : IComparer<IDrawningObject>
{
public int Compare(IDrawningObject x, IDrawningObject y)
{
if (x == null && y == null)
{
return 0;
}
if (x == null && y != null)
{
return 1;
}
if (x != null && y == null)
{
return -1;
}
var xCar = x as DrawningObjectArmCar;
var yCar = y as DrawningObjectArmCar;
if (xCar == null && yCar == null)
{
return 0;
}
if (xCar == null && yCar != null)
{
return 1;
}
if (xCar != null && yCar == null)
{
return -1;
}
if (xCar.GetArmoredCar.GetType().Name != yCar.GetArmoredCar.GetType().Name)
{
if (xCar.GetArmoredCar.GetType().Name == "DrawningArmoredCar")
{
return -1;
}
return 1;
}
var speedCompare = xCar.GetArmoredCar.ArmoredCar.Speed.CompareTo(yCar.GetArmoredCar.ArmoredCar.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xCar.GetArmoredCar.ArmoredCar.Weight.CompareTo(yCar.GetArmoredCar.ArmoredCar.Weight);
}
}
}

View File

@ -11,6 +11,8 @@ namespace ArmoredCar
internal class DrawningObjectArmCar : IDrawningObject
{
private DrawningArmoredCar _armCar = null;
public DrawningArmoredCar GetArmoredCar => _armCar;
public DrawningObjectArmCar(DrawningArmoredCar car)
{
_armCar = car;

View File

@ -206,5 +206,14 @@ namespace ArmoredCar
_setCars.Insert(DrawningObjectArmCar.Create(rec) as T);
}
}
/// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer"></param>
public void Sort(IComparer<T> comparer)
{
_setCars.SortSet(comparer);
}
}
}

View File

@ -114,6 +114,18 @@ namespace ArmoredCar
}
}
}
/// <summary>
/// Сортировка набора объектов
/// </summary>
/// <param name="comparer"></param>
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}