I'm trying to sort a string List by an order defined in another array. I know it's possible in a variety of ways, but I'm not sure how to do it efficiently. I need this to be able to handle a large unsorted list, with thousands of items. Here's what I came up with:
List<string> sortStringListByArray(List<string> unsortedList, string[] order)
{
List<string> sortedList = new List<string>();
for(int i = 0; i < order.Length; i++)
{
foreach(string s in unsortedList)
{
if(s.Equals(order[i]))
{
sortedList.Add(s);
}
}
}
return sortedList;
}
It works as expected, but it's definitely not efficient. Is there any way I can do this without iterating across both the list and the order?
Edit: Clarification
Thanks!
StopWatchand measure how long it takes to run that code. If it´s only a few nano-seconds, why bother for it?