You must Sign In to post a response.
  • Category: ASP.NET

    Order comma separated values of string

    Hi,

    I have a string of comma separated values which has numbers. i want these numbers to be sorted in ascending order.
    Please tell me how do i do the same.
    Thanks in advance.
  • #365285
    create an array for these values store each numeric value in the array and then sort the array
    Thanks & regards,
    Gaurav Kumar Arora
    Site Coordinator - DNS
    My blog - http://gaurav-arora.com

  • #365286
    Hi,

    string input = "1,9,3,4,3,9,1,5,6,78,";
    string[] stringArray = input.Split( new char[] { ',' },
    StringSplitOptions.RemoveEmptyEntries );
    int length = stringArray.Length;
    int[] intArray = new int[length];
    for (int i = 0; i < length; i++)
    {
    try
    {
    intArray[i] = Convert.ToInt32( stringArray[i] );
    }
    catch (Exception)
    {
    // ignore
    }
    }
    Array.Sort( intArray );



    Thanks & Regards,
    Deepika - Sr. Editor
    Webmaster, GujaratSpider
    http://angeldeeps.blogspot.com/
    "Experience is the name everyone gives to his mistakes"

  • #365287
    List<int> fish = new List<int>();
    fish.Add(1);
    fish.Add(456);
    fish.Add(4);
    fish.Add(44);
    fish.Add(24);
    // Sort fish alphabetically, in ascending order (A - Z)
    fish.Sort();

    foreach (int f in fish)
    {
    MessageBox.Show(f.ToString());
    }



    Thanks & Regards,
    Deepika - Sr. Editor
    Webmaster, GujaratSpider
    http://angeldeeps.blogspot.com/
    "Experience is the name everyone gives to his mistakes"


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.