DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

How To Sort String Array Using LINQ In C#

12.14.2011
| 5601 views |
  • submit to reddit
        A string[] array and use a LINQ query expression to order its contents alphabetically. Note that we are ordering the strings, not the letters in the strings. 

using System;
using System.Linq;
class Program
{
    static void Main()
    {
	string[] a = new string[] {"Indonesian","Korean","Japanese","English","German"};
	var sort = from s in a	orderby s select s;
	foreach (string c in sort)
	{
	    Console.WriteLine(c);
	}
    }
}
/*OUTPUT
English
German
Indonesian
Japanese
Korean
*/

<a href="http://www.java-forums.org/blogs/eclipse-ide/"><strong>Eclipse IDE</strong></a> and <a href="http://www.java-forums.org/blogs/eclipse/"><strong>Eclipse</strong></a>