A sortDicionary will be more efficient:
var sortDictionary = new Dictionary<string, int> { { ".pptx", 0 }, { ".ppt" , 1 }, { ".docx", 2 }, { ".doc" , 3 } }; var sortedList = list.OrderBy(i => { var s = Path.GetExtension(i); int rank; if (sortDictionary.TryGetValue(s, out rank)) return rank; return int.MaxValue;
So the search is O(1) , not O(# of extensions) .
In addition, if you have a large number of file names and a small number of extensions, it may be faster to do
var sortedList = list .GroupBy(p => Path.GetExtension(p)) .OrderBy(g => { int rank; if (sortDictionary.TryGetValue(g.Key, out rank)) return rank; return int.MaxValue;
This means that the sorting scale depends on the number of individual extensions at the input, and not on the number of elements at the input.
It also allows you to specify two extensions with the same priority.
source share