Сортировка

This commit is contained in:
Марат Заргаров 2022-12-20 21:16:48 +04:00
parent 11ba50c912
commit 528019b117
7 changed files with 190 additions and 19 deletions

View File

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoubleDeckerBus
{
internal class BusCompareByColor : 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 xBus = x as DrawningObjectBus;
var yBus = y as DrawningObjectBus;
if (xBus == null && yBus == null)
{
return 0;
}
if (xBus == null && yBus != null)
{
return 1;
}
if (xBus != null && yBus == null)
{
return -1;
}
var baseColorCompare = xBus.GetBus.Bus.BodyColor.ToString().CompareTo(yBus.GetBus.Bus.BodyColor.ToString());
if (baseColorCompare != 0)
{
return baseColorCompare;
}
if (xBus.GetBus.Bus is EntityDoubleDeckerBus xDoubleDeckerBus && yBus.GetBus.Bus is EntityDoubleDeckerBus yDoubleDeckerBus)
{
var extraColorCompare = xDoubleDeckerBus.BodyColor.ToString().CompareTo(yDoubleDeckerBus.BodyColor.ToString());
if (extraColorCompare != 0)
{
return extraColorCompare;
}
}
return 0;
}
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoubleDeckerBus
{
internal class BusCompareByType : 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 xBus = x as DrawningObjectBus;
var yBus = y as DrawningObjectBus;
if (xBus == null && yBus == null)
{
return 0;
}
if (xBus == null && yBus != null)
{
return 1;
}
if (xBus != null && yBus == null)
{
return -1;
}
if (xBus.GetBus.GetType().Name != yBus.GetBus.GetType().Name)
{
if (xBus.GetBus.GetType().Name == "DrawningBus")
{
return -1;
}
return 1;
}
var speedCompare = xBus.GetBus.Bus.Speed.CompareTo(yBus.GetBus.Bus.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xBus.GetBus.Bus.Weight.CompareTo(yBus.GetBus.Bus.Weight);
}
}
}

View File

@ -14,6 +14,7 @@ namespace DoubleDeckerBus
_bus = bus;
}
public float Step => _bus?.Bus?.Step ?? 0;
public DrawningBus GetBus => _bus;
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return _bus?.GetCurrentPosition() ?? default;

View File

@ -51,6 +51,8 @@
this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.buttonSortByType_Click = new System.Windows.Forms.Button();
this.buttonSortByColor_Click = new System.Windows.Forms.Button();
this.groupBoxInstruments.SuspendLayout();
this.groupBoxMaps.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
@ -59,6 +61,8 @@
//
// groupBoxInstruments
//
this.groupBoxInstruments.Controls.Add(this.buttonSortByColor_Click);
this.groupBoxInstruments.Controls.Add(this.buttonSortByType_Click);
this.groupBoxInstruments.Controls.Add(this.groupBoxMaps);
this.groupBoxInstruments.Controls.Add(this.buttonLeft);
this.groupBoxInstruments.Controls.Add(this.buttonUp);
@ -72,7 +76,7 @@
this.groupBoxInstruments.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxInstruments.Location = new System.Drawing.Point(608, 24);
this.groupBoxInstruments.Name = "groupBoxInstruments";
this.groupBoxInstruments.Size = new System.Drawing.Size(200, 634);
this.groupBoxInstruments.Size = new System.Drawing.Size(200, 685);
this.groupBoxInstruments.TabIndex = 0;
this.groupBoxInstruments.TabStop = false;
this.groupBoxInstruments.Text = "Инструменты";
@ -86,16 +90,16 @@
this.groupBoxMaps.Controls.Add(this.comboBoxSelectorMap);
this.groupBoxMaps.Location = new System.Drawing.Point(0, 22);
this.groupBoxMaps.Name = "groupBoxMaps";
this.groupBoxMaps.Size = new System.Drawing.Size(200, 359);
this.groupBoxMaps.Size = new System.Drawing.Size(200, 323);
this.groupBoxMaps.TabIndex = 10;
this.groupBoxMaps.TabStop = false;
this.groupBoxMaps.Text = "Карты";
//
// buttonDeleteMap
//
this.buttonDeleteMap.Location = new System.Drawing.Point(7, 298);
this.buttonDeleteMap.Location = new System.Drawing.Point(7, 283);
this.buttonDeleteMap.Name = "buttonDeleteMap";
this.buttonDeleteMap.Size = new System.Drawing.Size(187, 53);
this.buttonDeleteMap.Size = new System.Drawing.Size(187, 34);
this.buttonDeleteMap.TabIndex = 4;
this.buttonDeleteMap.Text = "Удалить карту";
this.buttonDeleteMap.UseVisualStyleBackColor = true;
@ -144,7 +148,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::DoubleDeckerBus.Properties.Resources.Left;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(52, 592);
this.buttonLeft.Location = new System.Drawing.Point(53, 640);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 9;
@ -156,7 +160,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::DoubleDeckerBus.Properties.Resources.Up;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(88, 556);
this.buttonUp.Location = new System.Drawing.Point(89, 604);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 8;
@ -168,7 +172,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::DoubleDeckerBus.Properties.Resources.Right;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(124, 592);
this.buttonRight.Location = new System.Drawing.Point(125, 640);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 7;
@ -180,7 +184,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::DoubleDeckerBus.Properties.Resources.Down;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(88, 592);
this.buttonDown.Location = new System.Drawing.Point(89, 640);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 6;
@ -189,7 +193,7 @@
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(5, 520);
this.buttonShowOnMap.Location = new System.Drawing.Point(6, 568);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(188, 29);
this.buttonShowOnMap.TabIndex = 5;
@ -199,7 +203,7 @@
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(5, 484);
this.buttonShowStorage.Location = new System.Drawing.Point(6, 532);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(188, 30);
this.buttonShowStorage.TabIndex = 4;
@ -209,7 +213,7 @@
//
// buttonRemoveBus
//
this.buttonRemoveBus.Location = new System.Drawing.Point(5, 450);
this.buttonRemoveBus.Location = new System.Drawing.Point(6, 498);
this.buttonRemoveBus.Name = "buttonRemoveBus";
this.buttonRemoveBus.Size = new System.Drawing.Size(188, 28);
this.buttonRemoveBus.TabIndex = 3;
@ -219,7 +223,7 @@
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(5, 421);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 469);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(188, 23);
@ -227,9 +231,9 @@
//
// buttonAddBus
//
this.buttonAddBus.Location = new System.Drawing.Point(5, 387);
this.buttonAddBus.Location = new System.Drawing.Point(6, 431);
this.buttonAddBus.Name = "buttonAddBus";
this.buttonAddBus.Size = new System.Drawing.Size(188, 28);
this.buttonAddBus.Size = new System.Drawing.Size(188, 32);
this.buttonAddBus.TabIndex = 1;
this.buttonAddBus.Text = "Добавить автобус";
this.buttonAddBus.UseVisualStyleBackColor = true;
@ -240,7 +244,7 @@
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 24);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(608, 634);
this.pictureBox.Size = new System.Drawing.Size(608, 685);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@ -265,14 +269,14 @@
// SaveToolStripMenuItem
//
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(141, 22);
this.SaveToolStripMenuItem.Text = "Сохранение";
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
//
// LoadToolStripMenuItem
//
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(141, 22);
this.LoadToolStripMenuItem.Text = "Загрузка";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
//
@ -284,11 +288,31 @@
//
this.saveFileDialog.Filter = "txt file | *.txt";
//
// buttonSortByType_Click
//
this.buttonSortByType_Click.Location = new System.Drawing.Point(7, 351);
this.buttonSortByType_Click.Name = "buttonSortByType_Click";
this.buttonSortByType_Click.Size = new System.Drawing.Size(186, 37);
this.buttonSortByType_Click.TabIndex = 11;
this.buttonSortByType_Click.Text = "Сортировка по типу";
this.buttonSortByType_Click.UseVisualStyleBackColor = true;
this.buttonSortByType_Click.Click += new System.EventHandler(this.buttonSortByType_Click_Click);
//
// buttonSortByColor_Click
//
this.buttonSortByColor_Click.Location = new System.Drawing.Point(7, 393);
this.buttonSortByColor_Click.Name = "buttonSortByColor_Click";
this.buttonSortByColor_Click.Size = new System.Drawing.Size(186, 32);
this.buttonSortByColor_Click.TabIndex = 12;
this.buttonSortByColor_Click.Text = "Сортировать по цвету";
this.buttonSortByColor_Click.UseVisualStyleBackColor = true;
this.buttonSortByColor_Click.Click += new System.EventHandler(this.buttonSortByColor_Click_Click);
//
// FormMapWithSetBuses
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(808, 658);
this.ClientSize = new System.Drawing.Size(808, 709);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxInstruments);
this.Controls.Add(this.menuStrip);
@ -332,5 +356,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button buttonSortByColor_Click;
private Button buttonSortByType_Click;
}
}

View File

@ -249,5 +249,25 @@ namespace DoubleDeckerBus
}
}
}
private void buttonSortByType_Click_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? String.Empty].Sort(new BusCompareByType());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void buttonSortByColor_Click_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? String.Empty].Sort(new BusCompareByColor());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
}
}

View File

@ -191,5 +191,9 @@ namespace DoubleDeckerBus
_setBuses.Insert(DrawningObjectBus.Create(rec) as T);
}
}
public void Sort(IComparer<T> comparer)
{
_setBuses.SortSet(comparer);
}
}
}

View File

@ -110,6 +110,13 @@ namespace DoubleDeckerBus
}
}
}
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}