0

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?

6
  • 7
    Why would you ever "want" to use bubblesort? Commented Sep 15, 2015 at 16:06
  • Do you have a List<People>? Commented Sep 15, 2015 at 16:08
  • 10
    Sort is not a problem here, your input is. You're parsing your input, but not storing it anywhere. Create a Person class, get a List<Person> and add all your data into it within first foreach. Sort that list instead of string[] you're trying to sort now. Commented Sep 15, 2015 at 16:08
  • Question looked like duplicate of stackoverflow.com/questions/1964234/… based on title, but turns out title is not related to post much. Commented Sep 15, 2015 at 16:14
  • 3
    @rmn36 He doesn't "want", his teacher "wants" :-). Have you noticed that the question started with "For an assignment"? :-) Commented Sep 15, 2015 at 16:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.