Bool sort. Bools can be sorted. When stored in a C# array or List, they can be ordered with Sort methods. This allows you to order your collection by true or false.
This is useful for promoting or demoting elements programmatically. A sorted list of bools helps us prioritize "true" results over "false" results.
First example. One use for sorting bools is reordering a List of objects. Sometimes, you need to mark some elements in the List as "low priority," pushing them to the bottom of the List.
Part 1 The example populates a List generic with 5 bool values. It displays the List in that state, and then sorts it with List.Sort method.
Part 2 The default Sort method orders the bools from False to True. This is just like ordering 0 to 1.
Part 3 The descending keyword in the example uses the LINQ query syntax, which is useful for sorting.
using System;
using System.Collections.Generic;
using System.Linq;
// Part 1: populate a List of bools.
var items = new List<bool>();
items.Add(true);
items.Add(false);
items.Add(false);
items.Add(false);
items.Add(true);
foreach (bool value in items)
{
Console.WriteLine("UNSORTED: {0}", value);
}
// Part 2: sort the list.
items.Sort();
foreach (bool value in items)
{
Console.WriteLine("SORTED: {0}", value);
}
// Part 3: sort descending.
var sorted = from item in items
orderby item descending
select item;
foreach (bool value in sorted)
{
Console.WriteLine("REVERSE SORTED: {0}", value);
}UNSORTED: True
UNSORTED: False
UNSORTED: False
UNSORTED: False
UNSORTED: True
SORTED: False
SORTED: False
SORTED: False
SORTED: True
SORTED: True
REVERSE SORTED: True
REVERSE SORTED: True
REVERSE SORTED: False
REVERSE SORTED: False
REVERSE SORTED: False
Example, bool property. Bool sorting can implement a selection algorithm. Only one element should be chosen, but no elements should be eliminated.
Here We create a List of TestData items. We then sort them with the IsImportant property—important items come before unimportant ones.
Info This approach can improve error handling as it does not eliminate any elements from consideration.
Finally We can exit the foreach-loop with a break or return when we find an item that matches our conditions.
using System;
using System.Collections.Generic;
using System.Linq;
class TestData
{
public bool IsImportant { get; set; }
public string Data { get; set; }
}
class Program
{
static void Main()
{
// Add data to the list.
var items = new List<TestData>();
items.Add(new TestData() { IsImportant = true, Data = "Bird" });
items.Add(new TestData() { IsImportant = false, Data = "Cat" });
items.Add(new TestData() { IsImportant = true, Data = "Human" });
// Sort by bool on class.
var sorted = from item in items
orderby item.IsImportant descending
select item;
// Put "important" items first.
foreach (var item in sorted)
{
Console.WriteLine("ITEM: " + item.IsImportant + "; " + item.Data);
}
}
}ITEM: True; Bird
ITEM: True; Human
ITEM: False; Cat
Discussion. To filter, you can add a bool property or field to your class, and then as sign that to true or false depending on whether you want to give the item priority.
So Use true to promote the class. Sort the objects with the LINQ query syntax, using the order by descending clause.
Summary. Bools are sorted from false to true. The descending sort will order them from true to false. True is essentially equal to 1, and false to 0.
Use boolean sorting for selection algorithms where some objects should be demoted. This helps when we do not want to eliminate items from consideration, but want to prioritize others.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Dec 1, 2024 (rewrite).