Sort string array with by length in CSharp

Description

The following code shows how to sort string array with by length.

Example


  /* w  ww .j  a v  a2s  .c  o m*/
  
  
    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class MainClass {
    public static void Main() {

        string[] words = { "abc", "bcd", "def","z" };

        var sortedWords =
            from w in words
            orderby w.Length
            select w;

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

The code above generates the following result.





















Home »
  C# Tutorial »
    LINQ »




Operator
Select
Where
OrderBy
Group
Join
Let
LINQ