OrderBy

Input: IEnumerable<TSource>
Lambda expression:TSource => TKey

The following query emits a sequence of names in alphabetical order:

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

class Program
{
    static void Main()
    {
        string[] names = { "Java", "C#", "Javascript", "SQL", "Oracle", "Python", "C++", "C", "HTML", "CSS" };

        IEnumerable<string> query = names.OrderBy(s => s);
        foreach(String s in query){
           Console.WriteLine(s);
        }
    }
}

The output:


C
C#
C++
CSS
HTML
Java
Javascript
Oracle
Python
SQL

The following sorts names by length:

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

class Program
{
    static void Main()
    {
        string[] names = { "Java", "C#", "Javascript", "SQL", "Oracle", "Python", "C++", "C", "HTML", "CSS" };

        IEnumerable<string> query = names.OrderBy(s => s.Length);

        foreach(String s in query){
           Console.WriteLine(s);
        }
    }
}

The output:


C
C#
SQL
C++
CSS
Java
HTML
Oracle
Python
Javascript

The following performs a case-insensitive sort:


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

class Program
{
    static void Main()
    {
        string[] names = { "Java", "C#", "Javascript", "SQL", "Oracle", "Python", "C++", "C", "HTML", "CSS" };

        names.OrderBy(n => n, StringComparer.CurrentCultureIgnoreCase);

        foreach (String s in names)
        {
           Console.WriteLine(s);
        }
    }
}

The output:


Java
C#
Javascript
SQL
Oracle
Python
C++
C
HTML
CSS
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.