Wednesday, 6 June 2012

Beginning .Net , C# Tips : Sort an array using LINQ

Using LINQ you are able to sort array.
Array may contains integer , string , double or char values.
Here are sample example for this.
In this example we have to arrays one is integer and second is string.

C# Example :
int[] arrayNumbers = new[] { 1, 2, 5, 9, 10, 1, 0, 9, 5, 6, 4, 3, 2 };
int[] arrayNumbersSorted = (from an in arrayNumbers orderby an select an).ToArray();

string[] arrayString = new[] { "banana", "apple", "mango" };
string[] arrayStringSorted = (from an in arrayString orderby an select an).ToArray();

VB.net Example :
Dim arrayNumbers As Integer() = {1, 2, 5, 9, 10, 1, 0, 9, 5, 6, 4, 3, 2}
Dim arrayNumbersSorted As Integer() = (From an In arrayNumbers Order By an Select an).ToArray()

Dim arrayString As String() = {"banana", "apple", "mango"}
Dim arrayStringSorted As String() = (From an In arrayString Order By an Select an).ToArray()

1 comment: