Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# LINQ Orderby - How does true/false affect orderby?

I was studying a bit of LINQ ordering as I have a list of Ids, and I need to order them sequentially. However, there are certain ids that need to take precedence over the standard ordering.

Given this C# code (which can be pasted into .NET Fiddle to test) the ordering works as I need it to, but I don't understand why a not (!) operator on a contains is giving me the correct ordering?

My expected ordering output is (5, 1, 2, 3, 4, 6, 7, 8, 9).

If I have a Contains in my ordering, shouldn't it give ordering priority to the rows that returned true? Instead it appears to give ordering priority to rows that return false.

using System.Linq; using System;  public class Program {   public static void Main()   {     var numbersToFilterBy = new [] {5, 11, 20};      var x = new [] {new XClass(){Id = 1}, new XClass(){Id = 9}, new XClass(){Id = 5}, new XClass(){Id = 3}, new XClass(){Id = 4}, new XClass(){Id = 2}, new XClass(){Id = 6}, new XClass(){Id = 8}, new XClass(){Id = 7}};      var trueData = (from data in x                    orderby !numbersToFilterBy.Contains(data.Id), data.Id                     select data).ToList();      foreach(var item in trueData){         Console.WriteLine(item.Id);   } }  public class XClass{     public int Id{get;set;}   } } 

What is the explanation as to why this happens?

like image 729
DotNet NF Avatar asked Aug 23 '16 06:08

DotNet NF


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

The OrderBy method will sort items in ascending order by default. Now, given that the numeric representation of a boolean is:

  • false = 0
  • true = 1

false values will naturally come first. If you want to reverse the order just use the descending keyword:

var trueData = (from data in x                orderby numbersToFilterBy.Contains(data.Id) descending, data.Id                 select data).ToList(); 
like image 127
Sebastian Siemens Avatar answered Oct 05 '22 01:10

Sebastian Siemens


Basically, false is earlier than true... think of them as false=0, true=1. This is in-keeping with the documentation for bool.CompareTo(bool).

If you want to prioritize "true" values to the start, just use OrderByDescending instead.

like image 24
Jon Skeet Avatar answered Oct 05 '22 00:10

Jon Skeet