string array sorted by the length each element

image_pdfimage_print
   
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class MainClass {
    public static void Main() {

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

        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);
        }
    }
}

    


This entry was posted in LINQ. Bookmark the permalink.