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
students.OrderByDescending(s => s.LastName).ThenBy(s => s.Degree).ThenBy(s => s.Grade).ThenBy(s => s.FirstName)