I have two lists of string, and I want to extract from each list the index if the string at current index is in the second list(and vice versa), the string cant match exactly or can be a shorthand of another list, for example, consider this two list
List<string> aList = new List<string> { "Id", "PartCode", "PartName", "EquipType" };
List<string> bList = new List<string> { "PartCode", "PartName", "PartShortName", "EquipmentType" };
in the above example, I want from aList
the indexes: 1,2,3
and from bList
indexes 0,1,3
indexes 1,2 from aList
are obvious the string matched completely, but the interesting part are "EquipType" and "EquipmentType" which match becuse "EquipType" is a shorthand of "EquipmentType"
but "PartName" is not a shorthand of "PartShortName" so there indexes are not needed
these is my code
List<string> aList = new List<string> { "Id", "PartCode", "PartName", "EquipType" };// 1, 2 , 3
List<string> bList = new List<string> { "PartCode", "PartName", "PartShortName", "EquipmentType" };//0, 1 ,3
List<int> alistIndex = new List<int>();
List<int> blistIndex = new List<int>();
for (int i = 0; i < aList.Count; i++)
{
string a = aList[i];
for (int j = 0; j < bList.Count(); j++)
{
string b = bList[j];
string bigger, smaller;
int biggerCount, smallerCount;
if (a.Length > b.Length)
{
bigger = a; smaller = b;
biggerCount = a.Length ; smallerCount = b.Length ;
}
else
{
bigger = b; smaller = a;
biggerCount = b.Length; smallerCount = a.Length ;
}
int countCheck = 0;
for (int k = 0; k < biggerCount; k++)
{
if (smaller.Length != countCheck)
{
if (bigger[k] == smaller[countCheck])
countCheck++;
}
}
if (countCheck == smaller.Length)
{
alistIndex.Add(i);
blistIndex.Add(j);
res = true;
break;
}
else
res = false;
}
}
alistIndex.ForEach(i => Console.Write(i));
Console.WriteLine(Environment.NewLine);
blistIndex.ForEach(i => Console.Write(i));
Console.ReadKey();
the above code works just fine and looks very similar to this solution
but if change the order of the second list like so
List<string> bList = new List<string> { "PartCode", "PartShortName", "PartName", "EquipmentType" };
i will get index 0, 1 and 3 (but i want 0 2 and 3)
should i check the distance for every pair and return the lowest? or should i work i a different method
Thanks
p.s i also found this GitHub, but i don't know if it will do the trick for me