Given a list of strings like so:
a/b/*/lol/cats
*/*
foo/bar/**
foo/bar/*/C/**
How would you sort them by specificity?
My naive approach (which works for the majority of my test cases) is as follows:
var sorted = wildCards
.OrderBy(c => c.HasCatchAll)
.ThenBy(c => c.NumWildCards)
.ThenByDescending(c => c.Pattern.Length);
In the above code the HasCatchAll
property indicates that the string has a ** at the end (the only valid place for a **).
My reasoning for above is that anything with a **
is less specific than a wild card without, and the more wild cards you have the less specific you are being. That said, the above will fail for:
*/hi/*/*
*/*/hi/*
Any help would be greatly appreciated.