I'm trying to sort several columns of a table depending of the previous sorted column. It works fine for the first two columns. But as soon as I sort the third column the second one loses its sort. As far as I know for now there must be a problem with my foreach loop. Here is my code for sorting:
public List<object> inhaltSortieren(List<object> zuSortierendeListe, Dictionary<string, SortierRichtung> sortierung)
{
IOrderedEnumerable<object> sortierteListe = null;
if (sortierung.First().Value == SortierRichtung.asc)
sortierteListe = zuSortierendeListe.OrderBy(x => x.GetType().GetProperty(sortierung.First().Key).GetValue(x, null));
else if (sortierung.First().Value == SortierRichtung.desc)
sortierteListe = zuSortierendeListe.OrderByDescending(x => x.GetType().GetProperty(sortierung.First().Key).GetValue(x, null));
bool first = true;
foreach (KeyValuePair<string, SortierRichtung> spalte in sortierung)
{
if (first)
{
first = false;
continue;
}
if (spalte.Value == SortierRichtung.asc)
sortierteListe = sortierteListe.ThenBy(x => x.GetType().GetProperty(spalte.Key).GetValue(x, null));
else if (spalte.Value == SortierRichtung.desc)
sortierteListe = sortierteListe.ThenByDescending(x => x.GetType().GetProperty(spalte.Key).GetValue(x, null));
}
return sortierteListe.ToList();
}
Any ideas?
Update: Maybe I add some further information:
- @param zusortierendeListe: this is the List I want to sort, it is a List of Objects
- @param sortierung: This is the direction I want to sort, ascending or descending
The objects themselves are Lists of Tabledata