[英]Sorting with OrderBy, ThenBy
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.
据我所知,我的foreach循环一定存在问题。 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: 更新:也许我添加了一些更多信息:
The objects themselves are Lists of Tabledata 对象本身是Tabledata的列表
You're passing in a Dictionary
; 你传入一本
Dictionary
; the order in which you get values out of a Dictionary
when you use it as an IEnumerable<KeyValuePair>
(as your foreach
loop does) is (probably) not the order in which you added them! 当你将它作为
IEnumerable<KeyValuePair>
(正如你的foreach
循环那样)使用它从Dictionary
获取值的顺序(可能) 不是你添加它们的顺序!
You'll need to use a List<KeyValuePair>
(or some other ordered IEnumerable<KeyValuePair>
) instead of the Dictionary
, or even create a custom class to hold the field and direction and pass a List
of that. 您需要使用
List<KeyValuePair>
(或其他一些有序的 IEnumerable<KeyValuePair>
)而不是Dictionary
,或者甚至创建一个自定义类来保存字段和方向并传递List
of that。
Also have a look here 还看看这里
Just to make your code a bit more clear. 只是为了让你的代码更清晰一些。 You can put everything into the for-each loop or leave it as it is, but then use sortierung.Skip(1) in your code, because you used the first entry already.
您可以将所有内容放入for-each循环中或保持原样,但在代码中使用sortierung.Skip(1),因为您已经使用了第一个条目。 I also changed the Dictionary argument to IEnumerable> according to the previous comment.
我还根据之前的评论将Dictionary参数更改为IEnumerable>。
object GetValue(object value, string name)
{
return value.GetType().GetProperty(name).GetValue(value, null);
}
public List<object> SortContent(List<object> listToSort, Tuple<string, SortDirection>[] sortInfos)
{
if (sortInfos == null || sortInfos.Length == 0)
return listToSort;
IOrderedEnumerable<object> sortedList = null;
foreach (var column in sortInfos)
{
Func<object, object> sort = x => GetValue(x, column.Key);
bool desc = column.Value == SortDirection.Descending;
if (sortedList == null)
sortedList = desc ? listToSort.OrderByDescending(sort) : listToSort.OrderBy(sort);
else
sortedList = desc ? sortedList.ThenByDescending(sort) : sortedList.ThenBy(sort);
}
return sortedList.ToList();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.