In my arrayList, i have number which are shown below:
1 2
1
2
2 1
2
1
2 3
2
3
1 2 3
1 2
3
3 1
3
1
1 3 2
1 3
2
2 3 1
2 3
1

now i want to sort some number.
For example :(i) 2 3 in ArrayList, i wan sort it to 1 2.
(ii) 2 3 1 in ArrayList, i wan sort it in 1 2 3.
may i know how to sort the number?

Recommended Answers

All 3 Replies

You'll need to convert each string into an array of values, sort that array, then rebuild the string.

I believe you've done all these things in previous posts, just not all together. Give it a try.

Check this example:

ArrayList aList = new ArrayList();
            aList.Add("1 2");
            aList.Add("2 1");
            aList.Add("2 3 1");
            aList.Add("2 4 1 3");

            ArrayList aListNew = new ArrayList();
            //you have to create a new array list, because you cannot modify the list in the loop!
            foreach(string item in aList)
            {
                //creating a int array (generic list):
                List<int> intList = item.Split(' ').ToList().ConvertAll<int>(c => Convert.ToInt32(c));
                //sorting the list:
                intList = intList.Select(s => Convert.ToInt32(s)).OrderBy(o => o).ToList();
                StringBuilder sb = new StringBuilder();
                for (int j = 0; j < intList.Count; j++)
                {
                    if ((j + 1) < intList.Count)
                        sb.Append(intList[j].ToString() + " ");
                    else
                        sb.Append(intList[j].ToString());
                }
                aListNew.Add(sb);
            }

thanks. i get it.
now if i take the arrayList that already sort to compare with my another sortedLIst.
my sortedLIst is show below:
(1): 4
(2): 4
(1 2): 3
(3): 4
(1 3): 3
(2 3): 3
(1 2 3): 2
my ArrayList is shown below:
1 2
1
2
1 2
2
1
2 3
2
3
1 2 3
1 2
3
1 3
1
3
i wan the output is like this:
1 2:3
1:4
2:4
1 2:3
2:4
1:4
2 3:3
2:4
3:4
1 2 3:2
1 2:3
3:4
1 3:3
1:4
3:4


hope anyone can help me correct my code..

richTextBox6.Clear();
            ArrayList arryKey = new ArrayList(richTextBox4.Lines);
            for (int i = 0; i < arryKey.Count; i++)
            {
                string temp = (string)arryKey[i];
                string[] numbers = temp.Trim().Split(' ');
                Array.Sort(numbers);
                arryKey[i] = String.Join(" ", numbers);
                richTextBox6.AppendText(arryKey[i].ToString()+"\n");
            }
 
            SortedList<string, double> sortList2 = new SortedList<string, double>();
            string text = "";

            foreach (string key in arryKey)
            {
                if (sortList.ContainsKey(key))
                {
                    if (!sortList2.ContainsKey(key))
                    {
                        sortList2.Add(key, sortList[key]);
                        text += String.Format("{0}:{1}\r\n", key, sortList2[key]);
                    }
                }
            }
           richTextBox5.Clear();
            richTextBox5.AppendText(text);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.