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

3 comments:

  1. I am glad it was helpful for you, Sudhir DBAKings!!

    ReplyDelete
  2. static void Main(string[] args)
    {

    int[] data = { 999, 9, 19, 555, 5,6, 18, 399,7,8,9,45,78,47,1,2 };
    Array.Sort(data);

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

    Console.ReadLine();
    }

    IWANT PRINT 5,6 7,8,9 1,2

    ReplyDelete