seUser
seUser

Reputation: 1103

How to sort the list order using Linq?

Below is the order of the Data that I get in the list object from other service.

Data in List

How can I sort the order of above list in the below order?

Level 0 Class
Level 0 S-Class
Level 1 Class
Level 1 S-Class
Level 2 Class
Level 2 S-Class
Level 0 Lesson
Level 0 S-Lesson
Level 1 Lesson
Level 1 S-Lesson
Level 2 Lesson
Level 2 S-Lesson

So far I have tried as below, but only getting the right order for ProgramId "E".

    myList.OrderByDescending(x => x.ProgramId== "E")
          .ThenBy(x => x.Level)
          .ThenBy(x => x.Special = True)
          .ThenBy(x => x.Special = False)
          .ThenByDescending(x => x.ProgramId== "B")
          .ThenBy(x => x.Level)
          .ThenBy(x => x.Special = True)
          .ThenBy(x => x.Special = False);

Below is the sorting logic that I am trying to apply:

Any help suggestion on this would be appreciated!

Upvotes: 2

Views: 179

Answers (3)

musefan
musefan

Reputation: 48435

This will produce the order you have requested:

myList = myList.OrderByDescending(x => x.ProgramId)
    .ThenBy(x => x.Level)
    .ThenBy(x => x.Special);

I have no idea why you think this doesn't work, but I have now tested it and it works exactly to your specification.

Here is a working example

Upvotes: 1

Andy Evans
Andy Evans

Reputation: 7176

.OrderBy(x => x.Level).ThenBy(x => x.ProgramDescription)

Upvotes: -1

David Pilkington
David Pilkington

Reputation: 13626

try this?

 myList.OrderByDescending(x => x.ProgramId)
              .ThenBy(x => x.Level)
              .ThenByDescending(x => x.Special)

Upvotes: 3

Related Questions