Dot Net Perls

Bool Sorting - C#

by Sam Allen

Problem

You want to sort your List or array of bools in C#. This allows you to order your collection by true or false, which is useful for promoting or demoting elements programmatically.

Solution: C# and sorting bools

One use I had for sorting bools was for reordering a List of file information structures. Sometimes, I needed to mark some elements in the List as 'bad', pushing them to the bottom of the List.

Example 1: sorting bools from true to false and the opposite

Your bools will have a default of false in a class or struct. With bools, the default Sort method will put false before true. This is similar to 0 to 1 ordering.

boolValueSort order, ascending
true1Last
false0First
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<bool> l = new List<bool>();
        l.Add(true);
        l.Add(false);
        l.Add(false);
        l.Add(false);
        l.Add(true);

        Console.WriteLine("Unsorted bools:");
        foreach (bool b in l)
        {
            Console.WriteLine(b);
        }
        // True
        // False
        // False
        // False
        // True

        l.Sort();
        Console.WriteLine("Sorted bools:");
        foreach (bool b in l)
        {
            Console.WriteLine(b);
        }
        // False
        // False
        // False
        // True
        // True

        var v = from b in l
                orderby b descending
                select b;
        Console.WriteLine("Reverse sorted bools:");
        foreach (bool b in v)
        {
            Console.WriteLine(b);
        }
        // True
        // True
        // False
        // False
        // False
    }
}

The example here first populates a List generic with 5 bool values. It displays the List in that state, and then sorts it with List.Sort method.

The default Sort method orders the bools from False to True. This is just like ordering 0 to 1.

Finally, the descending keyword in the example above utilizes the LINQ query syntax, which is extremely useful for sorting. It specifies a descending sort, and the final result is ordered from True to False.

Question: how can I use bools to filter objects?

You can add a bool property or field to your class, and then in your algorithm assign that to true or false depending on whether you want to give the item priority.

Use true to promote the class, and then sort the objects with the LINQ query syntax above, the descending sort. The opposite approach is also useful.

Information: priorities and fallback behaviors

One algorithm this is useful for is a selection where only one element should be chosen, but no elements should be eliminated. Elsewhere in your algorithm, mark the elements as good or bad with the bool.

Finally, you can select the elements using a test, starting with the elements not marked as 'bad'. This improves error handling as it doesn't actually eliminate any elements from consideration.

Summary: sorting with bools in C#

In C#, bools are sorted from false to true by default, and the descending sort will order them from true to false. Essentially, true is equal to 1, and false is equal to 0.

Use boolean sorting for selection algorithms where some objects should be demoted from consideration. This improves reliability but is also very simple.

Dot Net Perls
About
Sitemap
Algorithms
Alphanumeric Sorting
Anagram Method
A-Star Pathfinding
BinarySearch Algorithm on List
Directed Acyclic Word Graph
New
Occurrence Count of String
StartsWith String Examples
© 2008 Sam Allen. All rights reserved.