What is the best way to do – Given a string A, and collection of strings C, order the strings in the collection in the non decreasing order of the position of A in the strings.
For instance,
A= abc
C= [deabc, abc, dabc, dad]
Sorted C= [abc, dabc, deabc]
My idea is to iterate over the collection and put it in a HashMap/Dictionary with the position of A in C[i] as index. And then constructing the sorted collection from HashMap. This is not a homework problem. Just wanted to know the efficient way/algorithm of doing this. Any pointers will be helpful.
Here’s a simple way with LINQ:
Note that strings not containing A will be sorted at the beginning because
IndexOfreturns-1. Also, the behavior for strings with A at the same index is undefined, and will be returned in arbitrary order unless you provide a.ThenBysort to handle those.