0

I want to sort a list based on 3 numeric conditions. I need to do this sorting based on the property called 'CompletionPercentage'. So the list should be sorted on below conditions

  1. Filter the list where the percentage should be greater than 0 and less than 100
  2. Next filter the list where the percentage is equal to 0
  3. Next filter the list where the percentage is equal to 100

I have tried the below code but it is not working. Please help.

var data = myCollection
  .ToList()
  .Where(x => getCourses.CourseLevel.Contains(x.CourseLevel)
  .OrderBy(x => x.CompletionPercentage > 1 && x.CompletionPercentage < 99)
  .ThenBy(x => x.CompletionPercentage == 0)
  .ThenBy(x => x.CompletionPercentage == 100);
3
  • OrderBy and ThenBy are sorting operations, not filtering. If by filtering you mean "sorting", you might want to implement a custom IComparer. Can you clarify exactly what you want, perhaps with a worked example? Commented Aug 22, 2022 at 6:38
  • 1
    " but it is not working." can you be more precise? please post an input example, the expected output, but the most important part: the actual outcome. Commented Aug 22, 2022 at 6:42
  • what do you want to achieve, its not obvious because your words and code conflict? Commented Aug 22, 2022 at 6:56

1 Answer 1

2

I suggest mapping items into three groups as it's mentioned in the question:

.OrderBy(item => item.CompletionPercentage ==   0 ? 2
               : item.CompletionPercentage == 100 ? 3
               : 1)

Here we have running item (CompletionPercentage > 0 and CompletionPercentage < 100) be the 1st, then not started items (CompletionPercentage == 0) followed by completed items (CompletionPercentage == 100)

Code:

var data = myCollection
  .Where(item => getCourses.CourseLevel.Contains(item.CourseLevel)
  .OrderBy(item => item.CompletionPercentage ==   0 ? 2
                 : item.CompletionPercentage == 100 ? 3
                 : 1)
  .ThenBy(item => item.CompletionPercentage) // if you want order per cents as well 
  .ToList();            // if you want data to be List<T>
Sign up to request clarification or add additional context in comments.

2 Comments

Your code works fine. Thanks for your help! I tried the below piece of code which works too. But I will use code which is more readable. var data = myCollection.ToList().Where(x => getCourses.CourseLevel.Contains(x.CourseLevel) &&tempList.Contains(x.CompletionPercentage)).OrderBy(x => x.CompletionPercentage > 1 && x.CompletionPercentage < 100).OrderBy(x => x.CompletionPercentage == 0).OrderBy(x => x.CompletionPercentage == 100);
@CodeLearner: in your current code: 1. .ToList() is premature materialization which can be dropped. 2. Do not use several .OrderBy, but OrderBy followed by ThenBy, ThenBy. 3. Be careful with ranges: x.CompletionPercentage > 1 - what if x.CompletionPercentage == 1? 4. Check if tempList.Contains(x.CompletionPercentage) condition is actually required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.