Sorting in alphabetical order is simple; adding .OrderBy(s => s) before that .ToArray() . Keyword-based sorting is harder.
The quick and dirty way is to divide into three:
- Lines containing "Francais":
.Where(s => s.Contains("Francais") - Lines containing "England":
.Where(s => s.Contains("Anglais") - Else:
.Where(s => !francaisList.Contains(s) && !anglaisList.Contains(s))
Then you can sort each of them in alphabetical order and combine them.
Alternatively, you can implement IComparer using the logic you described:
For lines A and B:
- If A Contains "Francais"
- If B contains "Francais", alphabetical order
- Else
- If B contains "Francais", B goes first.
- Else
- If A contains "England",
- If B contains "England", alphabetical order
- Else, A goes first
- Else, alphabetical order
There may be room for logical reinstallation to simplify this. When all this logic is complete in a class that implements IComparer , you can specify this class to use .OrderBy() to order your query results based on your custom logic.
source share