Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C# Advanced Sorting with LINQ 2


  • Please log in to reply
No replies to this topic

#1 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 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 26 August 2011 - 12:59 PM

More LINQ Sorting

Previously we looked at sorting with LINQ and some more advanced sorts using multiple criteria. We are going to revisit this topic using queries for the sort instead of LINQ extension methods. We will use the same data set as last time to demonstrate how this works.
For this tutorial we will be dealing with person objects with the following fields:

  • Id
  • Age
  • Height
  • RegisteredDate
First, we need to create a C# class to represent all this information:

class Person    {
        public int Id { get; set; }
        public double Height { get; set; }
        public int Age { get; set; }
        public DateTime RegisteredDate { get; set; }
    }

The use of get and set automatically make a function to return the value and to set the value.
To demonstrate various ways of sorting we need a decent data set that we can use.

[TABLE="class: grid"]
[TR]
[TD]ID[/TD]
[TD]Age[/TD]
[TD]Height[/TD]
[TD]RegisteredDate[/TD]
[/TR]
[TR]
[TD]0[/TD]
[TD]3[/TD]
[TD]10[/TD]
[TD]2011-03-3[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]3[/TD]
[TD]12[/TD]
[TD]2012-08-4[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]5[/TD]
[TD]10[/TD]
[TD]2012-08-04[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]7[/TD]
[TD]8[/TD]
[TD]2010-04-05[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]6[/TD]
[TD]9[/TD]
[TD]2009-06-08[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]4[/TD]
[TD]6[/TD]
[TD]2008-05-04[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]5[/TD]
[TD]4[/TD]
[TD]2008-05-04[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]3[/TD]
[TD]10[/TD]
[TD]2008-05-04[/TD]
[/TR]
[/TABLE]



We will make use of the above data set. Now we need to create Person objects for each row and create an array of these objects for sorting.
Add this code to your main function:

var person1 = new Person {Id = 0, Age = 3, Height = 10, RegisteredDate = new DateTime(2011, 3, 3)};
var person2 = new Person {Id = 1, Age = 3, Height = 12, RegisteredDate = new DateTime(2012, 8, 4)};
var person3 = new Person {Id = 2, Age = 5, Height = 10, RegisteredDate = new DateTime(2012, 8, 4)};
var person4 = new Person {Id = 3, Age = 7, Height = 8, RegisteredDate = new DateTime(2010, 4, 5)};
var person5 = new Person {Id = 4, Age = 6, Height = 9, RegisteredDate = new DateTime(2009, 6, 8)};
var person6 = new Person {Id = 5, Age = 4, Height = 6, RegisteredDate = new DateTime(2008, 5, 4)};
var person7 = new Person {Id = 6, Age = 5, Height = 4, RegisteredDate = new DateTime(2008, 5, 4)};
var person8 = new Person {Id = 7, Age = 3, Height = 10, RegisteredDate = new DateTime(2008, 5, 4)};
var persons = new Person[] {person1, person2, person3, person4, person5, person6, person7, person8};

Sorting by age Ascending

This is simply a matter of using an order by query like this:
 var results = (from person in persons
                               orderby person.Age ascending
                          select person).ToList();

This isn’t anything we haven’t seen before. We are simply ordering the results by the age field in ascending order.

Sorting by Age Descending

The query to accomplish this:

 var results = (from person in persons
                               orderby person.Age descending
                          select person).ToList();

Sorting by Age Ascending and Height decreasing


We want to sort by age in increasing order and when two objects have equal age sort them in descending order of height. To do this we make use of the order by query again with a slight modification.
Query:


 var results = (from person in persons
                                  orderby person.Age ascending, person.Height descending
                                select person).ToList();

When outputting the results we get:

advancedsort3.jpg

Notice how we simply separate the various fields we want to sort by with a comma and specify how we want to sort. This applies to any number of fields.
Sorting by Age Ascending, Height Increasing and RegisteredDate Descending
In this case we are sorting by 3 fields. The query is:


var results = (from person in persons
                           orderby person.Age ascending, person.Height ascending, person.RegisteredDate descending 
                           select person).ToList();


When outputting the results we get the following:

advancedsort32.jpg

Notice that when age is increasing and when age is equal the heights are also in ascending order. Now also notice that when heights are equal the registered dates are ordered in descending order.

Edited by chili5, 26 August 2011 - 02:31 PM.

  • 0




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