Home

Sorting in LINQ

   

The Language Integrated Query

 

Introduction

Sorting a list consists of re-arranging its members in a certain order. The arrangement depends on the type of values. That is, the list can be arranged in alphabetical order, in ascending order, in chronological order, in incremental order, or in logical order.

When you create a list, you add the items in any order of your choice. When you create a select statement, the items are added to its list in the order they appear in the main list. When treating the new list or when presenting it to the user, you may want to arrange it in alphabetical, numerical, or chronological order. To support this operation, the LINQ provides the orderdy operator. To apply it, write the operator before the select operation followed by the from list. Here is an example:

using System;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;

public class Exercise : Form
{
    private ListBox lbxRegularOrder;
    private ListBox lbxSortedOrder;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        lbxRegularOrder = new ListBox();
        lbxRegularOrder.Location = new System.Drawing.Point(12, 12);
        lbxRegularOrder.Width = 100;

        lbxSortedOrder = new ListBox();
        lbxSortedOrder.Location = new System.Drawing.Point(120, 12);
        lbxSortedOrder.Width = 100;

        Text = "Numbers";
        MinimizeBox = false;
        MaximizeBox = false;
        Controls.Add(lbxRegularOrder);
        Controls.Add(lbxSortedOrder);

        Load += new EventHandler(ExerciseLoad);
        Size = new System.Drawing.Size(240, 145);
        StartPosition = FormStartPosition.CenterScreen;
    }

    private void ExerciseLoad(object sender, EventArgs e)
    {
        var numbers = new double[] { 12.44, 525.38, 6.28, 2448.32, 632.04 };

        var regularOrder = from n
                           in numbers
                           select n;

        foreach (var member in regularOrder)
            lbxRegularOrder.Items.Add(member.ToString());

        var sortedOrder = from n
                          in numbers
                          orderby n
                          select n;

        foreach (var member in sortedOrder)
            lbxSortedOrder.Items.Add(member.ToString());
    }

    [STAThread]
    public static int Main()
    {
        System.Windows.Forms.Application.Run(new Exercise());
        return 0;
    }
}

This would produce:

Numbers

If you apply the orderby operator simply followed by a variable, the list is ordered alphabetically or numerically depending on the types of values in the list. This is referred to as ascending. To re-enforce this, you can follow the variable with the ascending keyword. Here is an example:

var sortedOrder = from n
                  in numbers
                  orderby n ascending
                  select n;

After applying a where condition, you can sort the list using an orderby operator. For example, to get a list of odd numbers arranged in numerical order, you would write the statement as follows:

private void ExerciseLoad(object sender, EventArgs e)
{
    var numbers = new double[] { 12, 445, 25, 38, 61, 285, 2448, 32, 6320, 49 };

    var number = from n
                 in numbers
                 where n % 2 != 0
                 orderby n ascending
                 select n;

    foreach (var member in number)
        lbxNumbers.Items.Add(member.ToString());
}

This would produce:

Numbers

 
 
 

Descending Order

You can arrange a list in reverse ascending order, in decremental order, or in reverse chronological order. To support this, the LINQ uses the orderby keyword in combination with the descending keyword. Here is an example:

private void ExerciseLoad(object sender, EventArgs e)
{
    var numbers = new double[] { 12.44, 525.38, 6.28, 2448.32, 632.04 };

    var regularOrder = from n
                       in numbers
                       select n;

    foreach (var member in regularOrder)
        lbxRegularOrder.Items.Add(member.ToString());

    var sortedOrder = from n
                      in numbers
                      orderby n descending
                      select n;

    foreach (var member in sortedOrder)
        lbxSortedOrder.Items.Add(member.ToString());
}

This would produce:

Numbers

 
   
 

Previous Copyright © 2008-2014, FunctionX Next