Sort Integer and String Array in C#

Example 1: Sort Int[] Array
This example int[] array that you have in your C# program. You can call static Array.Sort Method and use it to sort a integer array in place. The result is an ascending order of numbers.

using System;
using System.Collections.Generic;
using System.Text;

class Program
{
        static void Main(string[] args)
        {

               int[] data = { 999,9,19,555,5,18,399};
               Array.Sort(data);

               foreach (int i in data)
                       Console.WriteLine(i);

               Console.ReadLine();
        }
}


Example 2: Sort string[] Array
This example string[] array that you have in your C# program. You can call static Array.Sort Method and use it to sort a string array in place. The result is an alphabetical sort.


using System;

class Program
{
    static void Main()
    {
             string[] str = new string[]
             {
                   "British",
                   "Indian",
                   "American",
                   "Chinese",
                   "Malaysians"
             };
             Array.Sort(str);

              foreach (string s in str)
                       Console.WriteLine(s);   

              Console.ReadLine();
}


Example 3: Sort string Array with LINQ
This example string[] array that you have in your C# program. You can call static Array.Sort Method and use it to sort a string array in place. The result is an alphabetical sort. We will use LINQ query expression to order its contents.

 using System;
using System.Linq;

class Program
{
    static void Main()
    {
             string[] str = new string[]
             {
                     "British",
                     "Indian",
                     "American",
                     "Chinese",
                     "Malaysians"
             };
             var sort = from s in str
                               orderby s
                               select s;

             foreach (string c in sort)
                   Console.WriteLine(c);

             Console.ReadLine();
    }
}


 
Example 4: Sort string Array in reverse using LINQ
This example string[] array that you have in your C# program. You can call static Array.Sort Method and use it to sort a string array in place. The result is an alphabetical decending sort. We will use LINQ query expression to order its contents.

using System;
using System.Linq;

class Program
{
    static void Main()
    {
              string[] str = new string[]
              {
                    "British",
                    "Indian",
                    "American",
                    "Chinese",
                    "Malaysians"
               };
               var desc = from s in str
                                   orderby s descending
                                   select s;

               foreach (string s in desc)
                       Console.WriteLine(s);
   
               Console.ReadLine();
    }
}


Example 5: Sort List
List<string> is a generic List collection of strings. It is stored as a string array. We can use LINQ exact same way we used in Example 3 and 4.

using System;
using System.Collections.Generic;
class Program
{
    static void Main()
    {
              List<string> lst = new List<string>()
             {
                      "British",
                      "Indian",
                      "American",
                      "Chinese",
                      "Malaysians"
              };
              lst.Sort();
              foreach (string s in lst)
                    Console.WriteLine(s);


              Console.ReadLine();
    }
}

Split String VB.NET Examples

How to Split String in VB.NET? There are different ways to call Split function in VB.NET. Below solutions guide you VB.NET Split string/sentence with examples.

Solution 1:

Simple Split Example. Below example uses Split on String.
' Input string
Dim str As String = "This is a test string"

' Split string based on spaces
Dim words As String() = str.Split(New Char() {" "c})

' Use For Each loop over words and display them. See the result below in Output.
Dim word As String
For Each word In words
          Console.WriteLine(word)
Next


Solution 2:
Split Parts of a file path in VB.NET. Below example that splits file path.
' file system path we need to split
Dim str As String = "C:\Users\TestUser\Documents\Doc"

' Split the string on the backslash character (\)
Dim parts As String() = str.Split(New Char() {"\"c})

' Loop through result strings with For Each.
Dim part As String
For Each part In parts
       Console.WriteLine(part)
Next


Solution 3:
Split string or sentence base on words in VB.NET
There are cases where you need to extract the words from a string/sentence in VB.NET. Below solution can handle non-word characters differently than the String Split method. For the same we need to use Regex.Split to parse the words in a string.

Add below line on top of your page.
Imports System.Text.RegularExpressions

Below code will splits words in VB.NET
Dim str As String
Dim arr As String() = SplitWordsInString("Oh! this is a simple task.")

For Each str In arr
        Console.WriteLine(str)
Next
Console.ReadLine()

Private Function SplitWordsInString (ByVal str As String) As String()   
          Return Regex.Split(str, "\W+")
End Function