89

I want to sort a list in C#.

Like where structure property AVC goes to true then show them first then AVC goes to false. Are any way to do this in C# LINQ?

bluish
  • 26,356
  • 27
  • 122
  • 180

4 Answers4

167

Well, the simplest way using LINQ would be something like this:

list = list.OrderBy(x => x.AVC ? 0 : 1)
           .ToList();

or

list = list.OrderByDescending(x => x.AVC)
           .ToList();

I believe that the natural ordering of bool values is false < true, but the first form makes it clearer IMO, because everyone knows that 0 < 1.

Note that this won't sort the original list itself - it will create a new list, and assign the reference back to the list variable. If you want to sort in place, you should use the List<T>.Sort method.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Is .ToList() necessary here? – Krishna P S Jan 19 '17 at 07:02
  • 1
    @KrishnaPS: Yes, assuming the compile-time type of `list` is `List`. Otherwise you'd be trying to assign an `IEnumerable` to a `LIst` variable. You don't need it if you just need a sequence which will be lazily evaluated every time you iterate over it though. – Jon Skeet Jan 19 '17 at 07:05
  • .Thank you! So order by returns a list which is lazily evaluated? Am I missing something here? Sorry that I am confused? I am facing a situation where I have ordered the list and returned it as IEnumerable as above, however when I do a for each on that returned list to change a value, I see that the order is disrupted.. – Krishna P S Jan 19 '17 at 07:45
  • @KrishnaPS: `OrderBy` returns a *sequence*, not a list. It sounds like you should be asking a new question demonstrating your problem though. – Jon Skeet Jan 19 '17 at 08:17
  • Kindly help me on this question that I have posted http://stackoverflow.com/questions/41738008/c-sharp-strange-behaviour-of-orderby-linq – Krishna P S Jan 19 '17 at 09:23
19

Like this?

In LINQ:

var sortedList = originalList.OrderBy(foo => !foo.AVC)
                             .ToList();

Or in-place:

originalList.Sort((foo1, foo2) => foo2.AVC.CompareTo(foo1.AVC));

As Jon Skeet says, the trick here is knowing that false is considered to be 'smaller' than true.

If you find that you are doing these ordering operations in lots of different places in your code, you might want to get your type Foo to implement the IComparable<Foo> and IComparable interfaces.

Ani
  • 111,048
  • 26
  • 262
  • 307
16

I assume that you want them sorted by something else also, to get a consistent ordering between all items where AVC is the same. For example by name:

var sortedList = list.OrderBy(x => c.AVC).ThenBy(x => x.Name).ToList();
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

A syntax alternative for the accepted answer:

list = from item in list
       orderby item.AVC
       select item;

Keep in mind that "list" is now an IEnumerable object and not a List one.

basquiatraphaeu
  • 525
  • 7
  • 19