I would order paths in descending way, and then just skip sub-paths when enumerating collection of paths and adding them to results:
string[] paths = { @"C:Users", @"C:Userscheese", @"D:Shadowstuff", @"D:Shadow" };
string currentPath = "";
List<string> result = new List<string>();
var comparer = StringComparer.InvariantCultureIgnoreCase;
foreach (var path in paths.OrderByDescending(p => p, comparer))
{
if (currentPath.IndexOf(path, StringComparison.InvariantCultureIgnoreCase) >= 0)
continue;
result.Add(path);
currentPath = path;
}
Result:
[
"D:\Shadow\stuff",
"C:\Users\cheese"
]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…