Click here to Skip to main content
15,120,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Concatenate Two List & sort concatenated list on the basis of first List.

Suppose First List Data is :
List1 = [21]
[24]
[23]
[26]

Suppose Second List Data is :
List2 = [21]
[32]
[38]
[26]
[25]

List1 and List2 after concatenation i.e
List3 = [21]
[24]
[23]
[26]
[21]
[32]
[38]
[26]
[25]

My query is I want result of the list after sorting in this Fashion : -
List3= [21]
[21]
[24]
[23]
[26]
[26]
[32]
[38]
[25]
Posted

C#
ArrayList al = new ArrayList();

       for (int i = 0; i < ListBox1.Items.Count; i++)
       {
           al.Add(ListBox1.Items[i].ToString());
       }
       for (int i = 0; i < ListBox2.Items.Count; i++)
       {
           al.Add(ListBox2.Items[i].ToString());
       }
       al.Sort();
       for (int i = 0; i < al.Count; i++)
       {
           ListBox3.Items.Add(al[i].ToString());
       }
   
Hi ,

Try this

C#
IList<int> List1 = new List<int>() { 5, 7, 1, 4 };
IList<int> List2 = new List<int>() { 3, 11, 9, 2 };
IList<int> Result = List1.Union(List2).OrderBy(p => p).ToList<int>();
   
Comments
Philip Stuyck 13-Aug-12 2:12am
   
You are using the union operator, won't it exclude doubles ? The right solution is to use the concat operator.
Prabhakaran Soundarapandian 13-Aug-12 2:20am
   
Thanks Philip...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900