Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare items between two disordered ListBox

Tags:

c#

asp.net

I have 2 ListBoxs which has a set of items. The count between each ListBoxs can be same or different, if the count is same, I want to check if items between the ListBoxs are same. The items can be disordered or ordered as shown below:

ListBox1 = { "C++", "C#", "Visual Basic" };
ListBox2 = { "C#", "Visual Basic", "C++" };

Kindly help.

like image 834
Archana B.R Avatar asked Dec 20 '22 16:12

Archana B.R


2 Answers

You could use Linq's All function

var ListBox1 = new string[] { "C++", "C#", "Visual Basic" };
var ListBox2 = new string[] { "C#", "Visual Basic", "C++" };
bool same = ListBox1.Length == ListBox2.Length 
   && ListBox1.All(s => ListBox2.Contains(s));
like image 155
Netricity Avatar answered Jan 06 '23 02:01

Netricity


You can simply use HashSet:

var hashSet1 = new HashSet<string> { "C++", "C#", "Visual Basic" };
var hashSet2 = new HashSet<string> { "C#", "Visual Basic", "C++" };

var result = hashSet1.SetEquals(hashSet2);
like image 20
Ayaro Avatar answered Jan 06 '23 03:01

Ayaro