First of all I would like to apologize for the code ( I am still learning) I am doing a windows form project with Visual Studio about a Airlinelane seat manager system and I have reached an impasse. Now, the problem is that I have an array of objects called [totSeats] which has been generated with a for loop adding a letter and a number together. What I want to achieve is that when I press the button "Sort by seat" So far I have tried all the conventional method for sorting in C# and also the The Alphanum Algorithm DaveKoelle but it does not work. Here is the Class where all the "logic happens" [EDIT] Someone pointed out about this post How to Sort this String in Ascending Alphanumeric But it is not working as I said I have tried lots of solutions but nothing seems to work. I don't have an output because the compiler generate this error when I tried to implement the solution on the overmentioned method " Cannot convert lambda expression to type array because is not a delegate type".
Now for the output the list needs to basically sort back again as it was originally(1A 2A 3A) ETC..
So by clicking the sort by name the list is rearranged by sorting the list by name, then if I click the sort by seat it needs to go back as it was originally following the order 1A 2A 3A and so on...
I hope this clarify :)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Airline_seat_Reservation
{
public partial class Form1 : Form
{
static int totalSeats = 200;
Seat[] totSeats = new Seat[totalSeats];
string[] firstClassLetter = { "A", "B", "C", "D" };
string[] econClassLetter = { "A", "B", "C", "D", "E", "F" };
int[] partyEco = { 1, 2, 3 };
int[] partyFirst = { 1, 2 };
string[] seatPositionEco = { "Windows", "Middle", "Aisle" };
string[] seatPositionFirst = { "Windows", "Aisle" };
// Binding datagrid to array
private CurrencyManager currencyManager = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int newPosition = 0;
for (int i = 1; i <= 35; i++)
{
if (i <= 5) // specify row for 1st class
{
for (int z = 0; z < 4; z++)
{
string[] positionFirst = { "Windows", "Aisle", "Aisle", "Window" };
totSeats[newPosition] = new Seat(i + firstClassLetter[z], "First", positionFirst[z]);
newPosition++;
}
}
else //specify row for economy class
{
for (int v = 0; v < 6; v++)
{
string[] economyPosition = { "Windows", "Middle", "Aisle", "Aisle", "Middle", "Window" };
totSeats[newPosition] = new Seat(i + econClassLetter[v], "Economy", economyPosition[v]);
newPosition++;
}
}
}
// Binding datagrid to array
currencyManager = (CurrencyManager)dataGridView.BindingContext[totSeats];
dataGridView.DataSource = totSeats;
}
//Method for Serialization.
private void serializeBttn_Click(object sender, EventArgs e)
{
Stream stream = File.Open("Passenger.dat", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, totSeats);
stream.Close();
MessageBox.Show("Data Saved to file Thank you!");
}
//Method for deserialization.
private void deserializeBttn_Click(object sender, EventArgs e)
{
try
{
Stream stream = File.Open("Passenger.dat", FileMode.Open);
BinaryFormatter bformatter = new BinaryFormatter();
//Reading Employee Information
totSeats = (Seat[])bformatter.Deserialize(stream);
stream.Close();
currencyManager = (CurrencyManager)dataGridView.BindingContext[totSeats];
dataGridView.DataSource = totSeats;
MessageBox.Show("Data Loaded from File");
dataGridView.Refresh();
}
catch (Exception ex)
{ MessageBox.Show(ex.Message, "ERROR reading file!"); }
}
private void exitBttn_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void sortPassenger_Click(object sender, EventArgs e)
{
try
{
Array.Sort(totSeats);
dataGridView.DataSource = null;
dataGridView.DataSource = totSeats;
}
catch (Exception u)
{
MessageBox.Show(u.Message, "List is empty, Nothing to sort");
}
}
private void sortSeat_Click(object sender, EventArgs e)
{
}
private void radioFirstClass_CheckedChanged(object sender, EventArgs e)
{
if (this.radioFirstClass.Checked)
{
comboPeople.DataSource = partyFirst;
comboSeat.DataSource = seatPositionFirst;
}
}
private void radioEconomyClass_CheckedChanged(object sender, EventArgs e)
{
comboPeople.DataSource = partyEco;
comboSeat.DataSource = seatPositionEco;
}
private void reserveButton_Click(object sender, EventArgs e)
{
String comboValue = comboSeat.Text;
System.Diagnostics.Debug.WriteLine(comboValue);
}
}
This is the class:
[EDITED]
public class SeatComparer : IComparer
{
public int Compare(object s1, object s2)
{
Seat temp = (Seat)s1;
Seat temp2 = (Seat)s2;
string pattern = "([0-9]+)([A-Za-z])";
string h1 = Regex.Match(temp.SeatNum, pattern).Groups[2].Value;
string h2 = Regex.Match(temp2.SeatNum, pattern).Groups[2].Value;
if (h1 != h2)
return h1.CompareTo(h2);
string t1 = Regex.Match(temp.SeatNum, pattern).Groups[1].Value;
string t2 = Regex.Match(temp2.SeatNum, pattern).Groups[1].Value;
return int.Parse(t1).CompareTo(int.Parse(t2));
}
}
I have tried everything i Know and researched a lot on the internet before asking here. I really do hope that someone can help me solve this deadlock! Thank you!
UPDATE! I am posting a different solution to sort which works and hopefully will help anyone which had or will have the same problem. This method will replace letters with number, then it will sort them and then the number is back replaced with the original letter previously removed.
private void Replace(Seat x)
{
x.SeatNum = x.SeatNum.Replace('A', '1');
x.SeatNum = x.SeatNum.Replace('B', '2');
x.SeatNum = x.SeatNum.Replace('C', '3');
x.SeatNum = x.SeatNum.Replace('D', '4');
x.SeatNum = x.SeatNum.Replace('E', '5');
x.SeatNum = x.SeatNum.Replace('F', '6');
}
private void sortBySeatBtn_Click(object sender, EventArgs e)
{
//Replace every SeatNum with a number
foreach (object o in aSeat)
{
Replace(((Seat)o));
}
Array.Sort(aSeat, Seat.sortBySeatID());//use sort method from Seat class
foreach (object o in aSeat)
{
string temp = ((Seat)o).SeatNum.Substring(0, ((Seat)o).SeatNum.Length - 1); // return all minus last digit
string last = ((Seat)o).SeatNum.Substring(((Seat)o).SeatNum.Length - 1); // return last digit used to compare
((Seat)o).SeatNum = temp; //assign temp to current seatNum
if (last == "1") // is last letter removed equal to 1
{
((Seat)o).SeatNum += "A"; //replace to originary A
}
if (last == "2")
{
((Seat)o).SeatNum += "B";
}
if (last == "3")
{
((Seat)o).SeatNum += "C";
}
if (last == "4")
{
((Seat)o).SeatNum += "D";
}
if (last == "5")
{
((Seat)o).SeatNum += "E";
}
if (last == "6")
{
((Seat)o).SeatNum += "F";
}
}
dataGridView1.DataSource = null;
dataGridView1.DataSource = aSeat;
}