实现集合的动态排序 [英] Implement dynamic sorting of a collection

查看:84
本文介绍了实现集合的动态排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,

我有一个可观察的汽车对象集合,然后绑定到列表框控件的ItemSource。

I have an observable collection of cars objects which qre then bind to an ItemSource of a list box control.

汽车对象属性定义如下:

The cars object properties is define around the following :

品牌,类型,马力,颜色

Brand, Type, Horse Power, Colors

然后应用程序需要发挥动力喜欢根据用户排序。通过首选排序,它定义在一个XML文件中,名称如Standard,Prefer1,prefer 2,..在那些nqme中,我将按顺序获取cqrs属性,然后我想要
来过滤

Then the application need to implmement a dynamique prefer sorting depending on users. By prefer sortiing it is define in an XML file with name like Standard, Prefer1, prefer 2,.. Inside those nqme I will hqve the cqrs property in order I would like then to be filter

例如:

标准过滤器列表:我将按以下列表顺序进行实例:


品牌,颜色,类型

Ex:
Standard filter list : I will have fo instance the following list order :
Brand, Color,Type

MyFilter列表:我将按以下列表顺序实例:类型,颜色,品牌  

MyFilter list : I will have fo instance the following list order : Type, Color, Brand  

因此,根据用户选择的列表,我需要使用Linq根据我将获得的项目列表对我的集合应用排序(在第一个例子中;颜色,类型

So depending on the list which is selected by the user, I need to apply a sort on my collection using Linq based on the list of items criteria I will get (in first exemple Brand ; Color,Type

任何其他不属于该过滤器项目首选项列表的数据都将按字母顺序排序

Any other data which are not part of that filter list of items preference will be order in alphabetical orders

在第一个样本上生成它可能是一些简单的谎言:

Bqsed on first sample it could be somethegin as simple liek the follwoing :

如果要先获取我的列表顺序,请更喜欢它ems然后按字母顺序排列所有其他数据

HOw to make my list filter in order to get my list order first by the prefer items and then all other data in alphabetical order

任何想法怎么办?

谢谢

推荐答案

你好wakefun,

Hi wakefun,

虽然我不知道你想要什么,但我想我可以给你一些提示。
我认为你似乎已经有了基本的想法。

Although I can not know exactly what you want, but I think I can give you some tips. I think it seems that you already have the basic idea.

在WPF中,支持这些功能的CollectionView,您可以使用效果本身数据过滤/排序/分组。

In WPF, the CollectionView that supports these functions, you can filter /sort /group collection with effect itself data.

To按字母顺序排序:

To order by alphabetical :

cv.SortDescriptions.Add(new SortDescription(Name, ListSortDirection.Ascending));

//或

cv.SortDescriptions.Add(new SortDescription(Name,ListSortDirection.Ascending));

cv.SortDescriptions.Add(new SortDescription(Name, ListSortDirection.Ascending));

过滤一些东西:

  cv.Filter = new Predicate<object>(dataFilter);

Linq排序数据(C#) https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/sorting-data

Linq Sorting Data (C#)https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/sorting-data

Linq过滤数据(C#)

Linq Filtering Data (C#)

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/filtering-data

无论哪种方式,您似乎已经拥有了解决方案, ll左边是C#实现。

Either way, you seem to already have your solution, all that's left is C # implementation.

 我相信有很多不同的方式。祝你好运!

 I am sure there are a lot of different way. Good luck!

最好的问候,

Bob


这篇关于实现集合的动态排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆