score:0

Accepted answer

Update as per Krish comment:

Arrange all sorted groups in ascending order by their first item

var results = StudentsList
    .OrderBy(x => x.Student)
    .GroupBy(x => x.GroupID)
    .SelectMany(x => x.OrderBy(y => y.Student))
    .ToList();

Update as per YoKidYo comment

var results = StudentsList
     .GroupBy(x => x.GroupID)
     .OrderByDescending(x => x.Key)
     .SelectMany(x => x.OrderBy(y => y.Student))
     .ToList();

foreach (var item in results)
{
    Console.WriteLine(item.GroupID + " : " + item.Student);
}

result :

3 : Adam
3 : Jacob
2 : Donal
2 : Olivi
1 : Jack
1 : John
1 : Peter

Old code

You can order by descending the Keys(groupId), and order by ascending student names inside each group, like the following code :

var results = StudentsList
     .GroupBy(x => x.GroupID)
     .OrderByDescending(x => x.Key)
     .Select(x => new { Group = "Group " + x.Key, Students = x.Select(y => y.Student).OrderBy(z => z).ToList() })
     .ToList();

I hope this will help you out.

score:1

So the Linq query has sorted the data for you but now you need to display it. I have tested and it displays your expected output. Code below:

  static void Main(string[] args)
    {

        List<StudentInfo> studentsList = new List<StudentInfo>();

        studentsList.Add(new StudentInfo { Student = "John", University = "ABC", GroupID = 1, IsQualified = false });
        studentsList.Add(new StudentInfo { Student = "Jack", University = "DEF", GroupID = 1, IsQualified = false });
        studentsList.Add(new StudentInfo { Student = "Peter", University = "GHI", GroupID = 1, IsQualified = false });
        studentsList.Add(new StudentInfo { Student = "Olivia", University = "ABC", GroupID = 2, IsQualified = false });
        studentsList.Add(new StudentInfo { Student = "Donald", University = "JKL", GroupID = 2, IsQualified = false });
        studentsList.Add(new StudentInfo { Student = "Adam", University = "GHI", GroupID = 3, IsQualified = false });
        studentsList.Add(new StudentInfo { Student = "Jacob", University = "ABC", GroupID = 3, IsQualified = false });

        var results = studentsList.OrderBy(x => x.Student)
            .GroupBy(x => x.GroupID);


        foreach (var group in results)
        {
            Console.WriteLine(group.Key);

            foreach (var student in group)
            {
                Console.WriteLine(student.Student);

            }

            Console.WriteLine(Environment.NewLine);
        }
    }

public class StudentInfo
{
    public string Student { get; set; }
    public string University { get; set; }
    public int GroupID { get; set; }
    public bool IsQualified { get; set; }
}

score:1

 var groupedStudents = studentsList.GroupBy(a => a.GroupID, (key, Students) => new
 {
     Key = key,
     StudentsList = Students.ToList()
 }).OrderByDescending(a => a.Key).ToList();
 foreach (var item in groupedStudents)
 {
     Console.WriteLine("Group " + item.Key + ":");
     foreach (var stdList in item.StudentsList.OrderBy(a => a.Student))
     {
         Console.WriteLine(stdList.Student);
     }
     Console.WriteLine();
 }

Output:

Group 3:
Adam
Jacob

Group 2:
Donald
Olivia

Group 1:
Jack
John
Peter

Related Articles