I have an arrayList with key which are shown below:
1
2
3
1 2
1 3
3 1
2 3
1 2 3
2 3 1

I have another sortesList with key and value which shown below:
1:4
2:4
3:4
1 2:3
1 3:3
2 3:4
1 2 3:4

if the key in arrayList which is same with the key in sortedlist,
then will come out the value.
my expected output is :
1:4
2:4
3:4
1 2:3
1 3:3
2 3:4
1 2 3:4
2 3 1:4

how i compare this 2 keys and display it in another new sortedList?


Hope someone can help me. my coding are shown below:

ArrayList arryKey = new ArrayList();
            double v;
            foreach (String key1 in sortList2.Keys)
            {
                arryKey.Add(key1);
                foreach (String key in arryKey)
                {
                    if (sortList.ContainsKey(key))
                    {
                        sortList.TryGetValue(key1, out v);
                       
                       richTextBox5.AppendText(key1 + "  " + v +"\n");
                    }
}
            }

Recommended Answers

All 6 Replies

Since 2 3 1 doesn't exist in the key/value sorted list, why is it in the output list and where did the value come from?

Otherwise you do something like:

var e = ArrayList.Cast<String>().Intersect(SortedList.Keys);
foreach(String key in e) {
    NewSortedList.Add(key, SortedList[key]);
}

Obviously you'll have to change the variable names as I used the class names in my example. And you'll have to ask yourself why you are using an ArrayList when List<T> is vastly superior.

sorry, 2 3 1:4 is not available, i type wrongly.
i had try ur code, but got errors, cant show the same keys with values.

Post your code, and make sure you include it inside [code] [/code] tags.

thanks for ur help.
now i can display the keys in the new sortedlist, but the value is not i want.
my coding and output are shown below. hope anyone can help me.

ArrayList arryKey = new ArrayList();
            foreach (string key in arryKey)
            {
                if (sortList.ContainsKey(key))
                {
                    if (!sortList2.ContainsKey(key)) sortList2.Add(key, sortList[key]);
                }
            }

            string text ="";

            foreach (string key in sortList2.Keys)
            {
                text += String.Format("{0}:{1}\r\n", key, sortList2[key]);
            }
            richTextBox5.Clear();
            richTextBox5.AppendText(text);

My output but the value is not i want.
1 2:1
2 1:1
1:1
2:1
1 3:1
2 3:1
1 2 3:1
3 1:1
3 2:1
1 3 2:1
2 3 1:1
3:1

for example, i want the output is :
1 2:3
1 2 in the arrayList is same with 1 2 in sortedlist, therefore the value should be 3.

post your code in [code] [/code] tags.

may i know how to compare the key in arrayList with the key in sortedList?
the keys in my arrayList are shown below:
1 2
1 3
2 1
2 3
1 2 3
3 1
3 2
1 3 2
2 3 1
1
2
1
3
2
1
2
3
1 2
3
3
1
3
2
1 3
2
2 3
1
I have another sortesList with key and value which shown below:
1:4
2:4
3:4
1 2:3
1 3:3
2 3:4
1 2 3:4

after arrayLIst compare with the soretedList, i wan show all the value which are preserve the order with the arrayList.
p/s : the keys in arrayLIst like 2 3 1 need same value with the keys 1 2 3in sortedList.

my expected output :
1 2:4
1 3:3
2 1:3
2 3:4
1 2 3:4
3 1:3
3 2:4
1 3 2:4
2 3 1:4
and etc..

richTextBox4.Clear();
            string AssR = output.Replace("->", " ");
            richTextBox4.AppendText(AssR);

            String[] split = (output).Split(new string[] { "\r\n\t", "->" }, StringSplitOptions.RemoveEmptyEntries);
            string[] split2 = new string[split.Length];
            for (int i = 0; i < split.Length; i++)
            {
                split2[i] = split[i];
                richTextBox4.AppendText(split2[i].ToString() + "\r\n");
            }

            ArrayList arryKey = new ArrayList(richTextBox4.Lines); // fill arraylist from rtb

            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.