Saturday 22 February 2014

Linq using Orderby

This sample uses orderby and ascending to sort a list of String from lowest to highest .


using System;
using System.Collections.Generic;
using System.Linq;

namespace SampleLinq
{
    class Program
    {
        static void Main(string[] args)
        {
            OrderByLinQ();
        }

        private static void OrderByLinQ()
        {
            List<string> objlist = new List<string> { "Sriram", "TMS", "Ajmal", "Abdul", "Azeez", "Retheesh", "Guru", "Bala", "das" };

            var sortedWords = from w in objlist
                              orderby w
                              select w;

            Console.WriteLine("The sorted list of words:");
            foreach (var w in sortedWords)
            {
                Console.WriteLine(w);
            }
            Console.Read();
        }
    }
}

This sample uses orderby and descending to sort a list of doubles from highest to lowest.



        private static void OrderByLinQ()
        {
            List<string> objlist = new List<string> { "Sriram", "TMS", "Ajmal", "Abdul", "Azeez", "Retheesh", "Guru", "Bala", "das" };

            var sortedWords = from w in objlist
                              orderby w descending
                              select w;

            Console.WriteLine("The sorted list of words:");
            foreach (var w in sortedWords)
            {
                Console.WriteLine(w);
            }
            Console.Read();
        }
    }