-2

I'm trying to sort some array elements (that have strings and ints) in C#. The Array.Sort method is what I'm trying to write at the moment.

Student[] students = new Student[] {
    new Student("Jane", "Smith", "Bachelor of Engineering", 6),
    new Student("John", "Smith", "Bachelor of Engineering", 7),
    new Student("John", "Smith", "Bachelor of IT", 7),
    new Student("John", "Smith", "Bachelor of IT", 6),
    new Student("Jane", "Smith", "Bachelor of IT", 6),
    new Student("John", "Bloggs", "Bachelor of IT", 6),
    new Student("John", "Bloggs", "Bachelor of Engineering", 6),
    new Student("John", "Bloggs", "Bachelor of IT", 7),
    new Student("John", "Smith", "Bachelor of Engineering", 6),
    new Student("Jane", "Smith", "Bachelor of Engineering", 7),
    new Student("Jane", "Bloggs", "Bachelor of IT", 6),
    new Student("Jane", "Bloggs", "Bachelor of Engineering", 6),
    new Student("Jane", "Bloggs", "Bachelor of Engineering", 7),
    new Student("Jane", "Smith", "Bachelor of IT", 7),
    new Student("John", "Bloggs", "Bachelor of Engineering", 7),
    new Student("Jane", "Bloggs", "Bachelor of IT", 7),
};

Array.Sort(students);
foreach (Student student in students) {
    Console.WriteLine("{0}", student);
}

I need to:

  • Last names in descending order, followed by... Degrees in ascending order, followed by... Grades in ascending order, followed by... First names in ascending order.

So that it returns:

Smith, Jane (Bachelor of Engineering) Grade: 6
Smith, John (Bachelor of Engineering) Grade: 6
Smith, Jane (Bachelor of Engineering) Grade: 7
Smith, John (Bachelor of Engineering) Grade: 7
Smith, Jane (Bachelor of IT) Grade: 6
Smith, John (Bachelor of IT) Grade: 6
Smith, Jane (Bachelor of IT) Grade: 7
Smith, John (Bachelor of IT) Grade: 7
Bloggs, Jane (Bachelor of Engineering) Grade: 6
Bloggs, John (Bachelor of Engineering) Grade: 6
Bloggs, Jane (Bachelor of Engineering) Grade: 7
Bloggs, John (Bachelor of Engineering) Grade: 7
Bloggs, Jane (Bachelor of IT) Grade: 6
Bloggs, John (Bachelor of IT) Grade: 6
Bloggs, Jane (Bachelor of IT) Grade: 7
Bloggs, John (Bachelor of IT) Grade: 7
1
  • 3
    This is a good use-case for Linq. students.OrderByDescending(s => s.LastName).ThenBy(s => s.Degree).ThenBy(s => s.Grade).ThenBy(s => s.FirstName) Commented Apr 14, 2019 at 12:08

1 Answer 1

0

Assuming your object looks like:

public class Student
{
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string Degree { get; set; }
    public int Grade { get; set; }
}

You can sort it using a Linq query:

var sortedStudents = students
    .OrderByDescending(s => s.LastName)
    .ThenBy(s => s.Degree)
    .ThenBy(s => s.Grade)
    .ThenBy(s => s.FirstName);

foreach (var student in sortedStudents)
{
    Console.WriteLine("{0}", student);
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.