Table of Contents
1. C# Lambda Functions and Introduction to LINQ
2. C# LINQ Sorting
Sorting data is a very common task in computer programs. We might want to order data by submission date to see the most recent orders or we might be interested in sorting people by name to implement binary search to find people quickly. Implementing our own sort function requires us to know the strengths of various sorting algorithms and testing time to ensure the sort will always work correctly. The sort function can become very complicated in complex situations.
In business we are interested in solving these problems quickly and don’t want to worry about the details. LINQ provides us a convenient method for sorting that hides all the details of how sorting works and will work in very complex situations.
In this tutorial, we will look at simple methods of sorting data.
First, let us look at simple sorting an array of integers in increasing order.
int[] v = {1, 3, 2, 4, 6, 8, 2, 7};
var increasingOrder = (from itm in v
orderby itm ascending
select itm).ToArray();
Console.WriteLine("Increasing order");
foreach (var itm in increasingOrder)
{
Console.WriteLine(itm);
}
Console.WriteLine();
The fragment of the query that we are interested in is “orderby itm ascending”. You can think of this as saying order the array v in ascending order.Increasing Order Using Lambda Functions
We can alternatively write this as:
int[] v = {1, 3, 2, 4, 6, 8, 2, 7};
var increasingOrder = v.OrderBy(k => k);
The OrderBy sorts in ascending order. The lambda function k => k can be thought of as the identity function. This function just sorts in order of the items in the array.
If we wanted to sort in descending order we would simply switch ascending to descending as follows:
int[] v = {1, 3, 2, 4, 6, 8, 2, 7};
var decreasingOrder = (from itm in v
orderby itm descending
select itm).ToArray();
Console.WriteLine("Decreasing order:");
foreach (var itm in decreasingOrder)
{
Console.WriteLine(itm);
}
Console.WriteLine();
We can alternatively write this using lambda functions like this: int[] v = {1, 3, 2, 4, 6, 8, 2, 7};
var decreasingOrder = v.OrderByDescending(k => k);
Filtering and Sorting
We can filter a collection and then sort the results. The idea is to use a Where clause and then follow this by a order by clause.
var increasingOrder = (from itm in v
where itm%2 == 0
orderby itm ascending
select itm).ToList();
This query selects all items that are even in v and then orders those in ascending order.This can also be written as:
var results = v.Where(itm => itm % 2 == 0).OrderBy(k => k).ToList();
We can chain as many calls together as we like. These function calls are evaluated left to right. First, v.Where(itm => itm % 2 == 0) is evaluated. It is important to realize that when this is executed we do not have an array we just have a query created that selects all even numbers.
So at this point the current query is:
from itm in v where itm % 2 == 0 select itm;
Then after orderby(k=>k) is evaluated we have the query:
from itm in v where itm % 2 == 0 orderby itm ascending select itm;
The query is only executed when we iterate over results. So by calling ToList() or ToArray() actually has the query executed.
We can also execute the query by doing this:
foreach (var itm in results)
Console.WriteLine(itm)
This tutorial covered sorting in ascending and descending order and doing filter and sort at the same time. Next tutorial we look at more complicated sorting.
Table of Contents
1. C# Lambda Functions and Introduction to LINQ
2. C# LINQ Sorting
Edited by chili5, 17 August 2011 - 12:42 PM.


Sign In
Create Account


Back to top









