For an assignment I have to sort a bunch of employees alphabetically by their department.
For example:
David
Hunter
1
Admin
John
Smith
11
Sales
Jane
Appleby
5
Accounts
Michelle
Page
12
Sales
Peta
Jones
13
Admin
should become
Jane
Appleby
5
Accounts
David
Hunter
1
Admin
Peta
Jones
13
Admin
John
Smith
11
Sales
Michelle
Page
12
Sales
I want to use a bubblesort for this but I have no idea how to get it to sort by anything other than the first name. This is what I have so far:
string[] people = record.Split(new string[] { "\r\n\r\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string person in people)
{
string[] details = person.Split('\n');
firstName = details[0];
lastName = details[1];
empID = details[2];
department = details[3];
}
for (int i = 1; i < people.Length; i++)
{
for (int j = 0; j < people.Length - i; j++)
{
//people.department doesn't work
if (people.department[j].CompareTo(people.department[j + 1]) > 0)
{
temp = people[j];
people[j] = people[j + 1];
people[j + 1] = temp;
}
}
}
Is it possible to do what I'm trying to do with a bubblesort like this, and access the department of an employee? Should I consider other ways that might be better/ more simple?
List<People>?Personclass, get aList<Person>and add all your data into it within firstforeach. Sort that list instead ofstring[]you're trying to sort now.