Home > Software engineering >  Order by Id then by age
Order by Id then by age

Time:03-14

Ordering list of objects is giving me unwanted result, why can't I order list by id (integer) then by age (integer)? It only orders by Id and when using thenBy after, nothing changes. But when I'm using orderBy name first and thenBy age, it orders list.

// Student collection
    IList<Student> studentList = new List<Student>() { 
            new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
            new Student() { StudentID = 2, StudentName = "Steve",  Age = 15 } ,
            new Student() { StudentID = 3, StudentName = "Bill",  Age = 25 } ,
            new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,
            new Student() { StudentID = 5, StudentName = "Ron" , Age = 19 } ,
            new Student() { StudentID = 6, StudentName = "Ram" , Age = 21 } ,
            new Student() { StudentID = 7, StudentName = "Ram" , Age = 25 } ,
            new Student() { StudentID = 8, StudentName = "Bill",  Age = 19 } ,
        };
var thenByResult = studentList.OrderBy(s => s.StudentName).ThenBy(s => s.StudentID).ThenByDescending(s => s.Age);

foreach (var std in thenByResult)
        Console.WriteLine("Id: {0}, Name: {1}, Age:{2}",std.StudentID, std.StudentName, std.Age);
    

If I order by Id then by age I get:

enter image description here

What I need is age to be ordered by descending order, which in this case it doesn't work at all, only if I remove order thenby student id I get descending order of age, but I don't get the id ordering.

CodePudding user response:

You can't order by Age if you are already ordering by StudentId with unique Id.

CodePudding user response:

Perhaps you rather want to order by Age first, then by Id?

studentList.OrderByDescending(s => s.Age).ThenBy(s => s.StudentID);

CodePudding user response:

You firstly sort by StudentName field.
And your order is StudentName -> Id -> Age
To sort only by Id and Age try this:

studentList.OrderBy(s => s.StudentID).ThenByDescending(s => s.Age);

CodePudding user response:

You are not ordering by StudentID and then by Age descending, but by StudentName, then by StudentID and then by Agedescending.

If you do not want to order by StudentName, but only by StudentID and Age, you need to change your query like this:

var thenByResult = studentList.OrderBy(s => s.StudentID).ThenByDescending(s => s.Age);

CodePudding user response:

You change your code from

 var thenByResult = studentList.OrderBy(s => s.StudentName).ThenBy(s => s.Age).ThenByDescending(s => s.Age);

to

var thenByResult = studentList.OrderBy(s => s.StudentName).ThenBy(s => s.Age).ThenByDescending(s => s.Age).ThenBy(s=>s.StudentID);

CodePudding user response:

You should order your query like this as you are in need of Age to be in descending order,

var thenByResult = studentList.OrderBy(s => s.StudentID).ThenByDescending(s => s.Age);

This might do the trick for you.

CodePudding user response:

There's inconsistencies with the requirements:

  1. The title says "order by id then by age" (and assuming that the Id is unique then you shouldn't need to order again after it).

  2. The body says what you need is age to be ordered by descending order.

  3. The code first orders by student name, then by id, then by age (which again is useless at this point assuming the id is unique).

The assumption is that you want to first sort by age descending, and then if there are multiple students with the same age, they would be sorted by id.

var thenByResult = studentList.OrderByDescending(s => s.Age).ThenBy(s => s.StudentID);
  • Related