locked
How to sort string array? RRS feed

  • Question

  • User-1673382244 posted

    Hi

    any one help me

     

    string [] name={"ruban","markas","loiz,","sachin"}


    how to sort the above string array
    Wednesday, August 24, 2011 6:42 AM

Answers

  • User-1516073966 posted

    Try below:

    string[] name = { "ruban", "markas", "loiz,", "sachin" };
    
                for (int index = 0; index < name.Length; index++)
                {
                    for (int jIndex = index + 1; jIndex < name.Length; jIndex++)
                    {
                        if (string.Compare(name[index], name[jIndex]) > 0)
                        {
                            string temp = name[index];
                            name[index] = name[jIndex];
                            name[jIndex] = temp;
                        }
                    }
                }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 24, 2011 8:00 AM

All replies

  • User-1516073966 posted

    Try below:

     string[] name = { "ruban", "markas", "loiz,", "sachin" };
                Array.Sort(name);
    Wednesday, August 24, 2011 6:44 AM
  • User-68639941 posted

    namespace Bubble_Sort
    {
        class Program
        {
            static void Main(string[] args)
            {
                int n = 5;
                int[] arr = new int[n];
                Console.WriteLine("\n--------------------------");
                Console.WriteLine("Enter the element :");
                Console.WriteLine("\n--------------------------");
                for (int i = 0; i < n; i++)
                {
                    Console.WriteLine("<" + (i + 1) + ">");
                    arr[i] = Int32.Parse(Console.ReadLine());
                }
                for (int pass = 1; pass <= n - 1; pass++)
                {
                    for (int j = 0; j <= n - 2; j++)
                    {
                        if (arr[j] > arr[j + 1])
                        {
                            int Swap;
                            Swap = arr[j];
                            arr[j] = arr[j + 1];
                            arr[j + 1] = Swap;
                        }
                    }
                }
                Console.WriteLine("\n--------------------------");
                Console.WriteLine("The Sorted Element :");
                Console.WriteLine("\n--------------------------");
                for (int i = 0; i < n; i++)
                {
                    Console.WriteLine(arr[i]);
                }
                Console.ReadLine();
            }
        }
    }

    Refer : http://www.dotnetspider.com/resources/30548-Bubble-Sort-C.aspx

    hi, you can use

    Array.Sort(name);

    http://www.csharp-examples.net/sort-array/

    Wednesday, August 24, 2011 6:44 AM
  • User-662335483 posted

    Hi

    any one help me

     

    string [] name={"ruban","markas","loiz,","sachin"}


    how to sort the above string array

    http://www.dotnetperls.com/sort-string-array

    http://www.rgagnon.com/javadetails/java-0343.html

    Wednesday, August 24, 2011 6:51 AM
  • User1339196 posted

    Try like this 

      string[] name = { "ruban", "markas", "loiz,", "sachin" };

    Array.Sort(name);
    
    
    it will come like this
    loiz,
    markas,
    ruban,
    sachin
    Wednesday, August 24, 2011 7:30 AM
  • User-1673382244 posted

    thanks to all pls use for loop

    string [] name={"ruban","markas","loiz,","sachin"}


    how to sort the above
    string array
    Wednesday, August 24, 2011 7:43 AM
  • User-1062158585 posted

    int[] keys = new int[4];
    keys[0] = 11;
    keys[1] = 3;
    keys[2] = 8;
    keys[3] = 5;
    string[] names = new string[4];
    names[0] = "Howard, Ryan";
    names[1] = "Allen, Ray";
    names[2] = "Pujols, Albert";
    names[3] = "Iverson, Allen";
    Array.Sort(keys, names, 1, 2);

    integers

    2 int[] numbers = { 4, 3, 8, 0, 5 };

                Array.Sort(numbers);

    3. strings

    string [] name={"ruban","markas","loiz,","sachin"} 

        Array.Sort(name);

    Wednesday, August 24, 2011 7:45 AM
  • User1992938117 posted

    How to sort string array? using for loop

    No need to use loop use inbuild function Array.Sort(ArrayName)

    See the demo code:

    string[] name = new string[]
    	{
    	    "ruban",
    	    "markas",
    	    "loiz",
    	    "sachin"
    	};
    	Array.Sort(name);
    	foreach (string s in name)
    	{
    	    Console.WriteLine(s);
    	}
    Wednesday, August 24, 2011 7:57 AM
  • User2131854062 posted
    <script language="VB" runat="server">
      Sub Page_Load(sender as Object, e as EventArgs)
        Dim randNumbers(10) as Integer
        Dim i as Integer
        Dim rndNum as New Random()
        
        For i = 0 to 10
          randNumbers(i) = rndNum.Next(100)
        Next i
        
        ' Display the random numbers
        randNumDisplay.DataSource = randNumbers
        randNumDisplay.DataBind()
        
        ' Sort the array
        Array.Sort(randNumbers)
        
        ' Display the sorted array
        orderedNumDisplay.DataSource = randNumbers
        orderedNumDisplay.DataBind()
      End Sub
    </script>
    
    <b>Unsorted List of Random Numbers</b><br />
    <asp:DataGrid runat="server" id="randNumDisplay" ShowHeader="False" />
    <p>
    <b>Sorted List of Random Numbers</b><br />
    <asp:DataGrid runat="server" id="orderedNumDisplay" ShowHeader="False" />    


    http://www.4guysfromrolla.com/articles/060403-1.aspx
    Wednesday, August 24, 2011 7:58 AM
  • User-1516073966 posted

    Try below:

    string[] name = { "ruban", "markas", "loiz,", "sachin" };
    
                for (int index = 0; index < name.Length; index++)
                {
                    for (int jIndex = index + 1; jIndex < name.Length; jIndex++)
                    {
                        if (string.Compare(name[index], name[jIndex]) > 0)
                        {
                            string temp = name[index];
                            name[index] = name[jIndex];
                            name[jIndex] = temp;
                        }
                    }
                }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 24, 2011 8:00 AM