none
Sorting a list with multiple numbers RRS feed

  • Question

  • Hi i need a list that can sort itself numerically and with multiple numbers. 

    At the moment my code sorts the list alphabetically as following: 1,10,2,3... How can i change it so it sorts: 1,2,3,10?

    My code at the moment is:

    private void Sortera()  
            {
                string[] Lista = new string[lbxTopp.Items.Count];
                for (int i = 0; i < lbxTopp.Items.Count; i++)
                {
                    Lista[i] = lbxTopp.Items[i].ToString();
                }


                Array.Sort(Lista);
                Array.Reverse(Lista); 
                lbxTopp.Items.Clear();
                for (int i = 0; i < Lista.Length; i++)

                {
                    lbxTopp.Items.Add(Lista[i]);
                }

            }

                            
    Friday, March 9, 2018 4:33 PM

Answers

  • You could do something like the following.

    var stringValues = new[] {"23","1","100","45","101", "5"};
    var myInts = Array.ConvertAll(stringValues, s => int.Parse(s));
    Array.Sort(myInts);
    Console.WriteLine(string.Join(Environment.NewLine,myInts));

    Results

    1
    5
    23
    45
    100
    101


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, March 9, 2018 4:44 PM
    Moderator
  • Hi BaGGelitO,

    Continuing from Karen's answer, Your list box item got sorted in alphabetical order because item in ListBox are "String Format" you need to convert them into "Integer" then sort them in ascending order. Please look into below code it is also an another to achieve you goal, specially the Bold line. Hope this helps you.

            private void Form1_Load(object sender, EventArgs e)
            {
                ArrayList q = new ArrayList(); 
                foreach (object o in listBox1.Items)
                {
                    q.Add(Convert.ToInt32(o));
                } 
                q.Sort(); 
                listBox1.Items.Clear();
                foreach(object o in q)
                {
                    listBox1.Items.Add(o); 
                }
    
    
            }


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Monday, March 12, 2018 10:16 AM
    Moderator

All replies

  • You could do something like the following.

    var stringValues = new[] {"23","1","100","45","101", "5"};
    var myInts = Array.ConvertAll(stringValues, s => int.Parse(s));
    Array.Sort(myInts);
    Console.WriteLine(string.Join(Environment.NewLine,myInts));

    Results

    1
    5
    23
    45
    100
    101


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, March 9, 2018 4:44 PM
    Moderator
  • You can use OrderBy  using Linq for this:

    var sortedLista =  Lista.OrderBy(x=> Convert.ToInt32(x)); 

    and now print them to see :

    foreach(var item in sortedLista)
       Console.WriteLine(item);

    You would need to include a using for this:

    using System.Linq;


    Hope it helps!


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites


    Friday, March 9, 2018 5:04 PM
  • So you are getting your list of ints based on user input. You need to change your code a bit here.

    private void Sortera()  
            {
                string[] Lista = new string[lbxTopp.Items.Count];
                for (int i = 0; i < lbxTopp.Items.Count; i++)
                {
                    Lista[i] = lbxTopp.Items[i].ToString();
                }
                // you could either convert the string array to an int array with a foreach //statement or like Karen Payne suggested.

               int[] int_values = Array.ConvertAll(Lista, s => int32.Parse(s));

               Array.Sort(int_values, Lista); //Sort the int_values and Lista based on the //int_values. Lista will be ordered as a parallel array.

                //Array.Sort(Lista);
                //Array.Reverse(Lista); 
                lbxTopp.Items.Clear();
                for (int i = 0; i < Lista.Length; i++)

                {

    //You could also only order the int_values array and just have //lbxTopp.Items.Add(int_values[i].ToString()); instead

                    lbxTopp.Items.Add(Lista[i]);
                }

            }



    Friday, March 9, 2018 5:06 PM
  • Hello BaGGelitO,

    Try the bubble sort algorithm.

            private void BubbleSort(string[] arr)
            {
    
                for (int write = 0; write < arr.Length; write++)
                {
                    for (int sort = 0; sort < arr.Length - 1; sort++)
                    {
                        int front = int.Parse(arr[sort]);
                        int behind = int.Parse(arr[sort + 1]);
                        if (front > behind)
                        {
                            arr[sort + 1] = front.ToString();
                            arr[sort] = behind.ToString();
                        }
                    }
                }
            }

    Test code

     Random random = new Random();
                string[] Lista = new string[15];
                for (int i = 0; i < 15; i++)
                {
    
                    int re = random.Next(15);
                    Lista[i] = re.ToString();
                }
    
                BubbleSort(Lista);

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 12, 2018 8:00 AM
    Moderator
  • Hi BaGGelitO,

    Continuing from Karen's answer, Your list box item got sorted in alphabetical order because item in ListBox are "String Format" you need to convert them into "Integer" then sort them in ascending order. Please look into below code it is also an another to achieve you goal, specially the Bold line. Hope this helps you.

            private void Form1_Load(object sender, EventArgs e)
            {
                ArrayList q = new ArrayList(); 
                foreach (object o in listBox1.Items)
                {
                    q.Add(Convert.ToInt32(o));
                } 
                q.Sort(); 
                listBox1.Items.Clear();
                foreach(object o in q)
                {
                    listBox1.Items.Add(o); 
                }
    
    
            }


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Monday, March 12, 2018 10:16 AM
    Moderator