•

Check out our Community Blogs

Register and join over 40,000 other developers!

### Recent Blog Entries

• phi

I love this community !

• JackJames

hi i am jack i am seo expert jack james would love you to read new post

# C# LINQ Sorting

2 replies to this topic

### #1 chili5

chili5

CC Mentor

• Expert Member
• 3038 posts
• Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
• Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 13 August 2011 - 04:44 PM

LINQ Sorting

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.

1. C# Lambda Functions and Introduction to LINQ
2. C# LINQ Sorting

Edited by chili5, 17 August 2011 - 12:42 PM.

• 0

### #2 John

John

CC Mentor

• Moderator
• 4450 posts
• Location:New York, NY

Posted 16 August 2011 - 11:42 AM

What is LINQ? Is it just like a library or something?
• 0

### #3 chili5

chili5

CC Mentor

• Expert Member
• 3038 posts
• Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
• Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 16 August 2011 - 12:47 PM

Basically. LINQ Is a namespace in C# that lets you write queries on data structures... similar to how you would in SQL. The key here is the two interfaces: IQueryable and IEnumerable. IEnumerable is the interface for all collections that result from a LINQ query. Then IQueryable is the interface for all classes that represent queries. Because of the use of interfaces we can easily write linq queries for any structure that implements IEnumerable. In C# this is basically all of them.

With the EntityFramework we can write LINQ queries to databases instead of having to deal with SQL. They are similar but there are benefits to LINQ over SQL.
• 0

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download