WPF 列表<t> 排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18525253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:32:24  来源:igfitidea点击:

WPF list<t> sort

wpfcollections

提问by Drew

my first post here.

我在这里的第一篇文章。

i have a list"<"frameworkelement> that i'm populating with a select process. each frameworkelement has a uid that holds its ZOrder.

我有一个列表"<"frameworkelement>,我正在用一个选择进程填充它。每个框架元素都有一个 uid 来保存它的 ZOrder。

i need to sort these by the ZOrder from lowest to highest. i can get this using a listbox and adding the Uid's like this:

我需要按 ZOrder 从低到高对这些进行排序。我可以使用列表框并像这样添加 Uid 来获取它:

            //Add Object Uid's
            ListBox lstTempOrder = new ListBox();
            foreach(FrameworkElement feObject in MainWindow.Data.SelectedObjects)
            {
                lstTempOrder.Items.Add(feObject.Uid);
            }
            //Reorder from 0 to above of the ZIndexes       
            lstTempOrder.Items.SortDescriptions.Add(new System.ComponentModel.SortDescription("", System.ComponentModel.ListSortDirection.Ascending));

but i need to do this with a List"<"FrameWorkElement> and Sort.

但我需要用 List"<"FrameWorkElement> 和 Sort 来做到这一点。

Here is the code where i populate the List"<"T> (SelectedObjects and CopyObjectsCollections are List"<"FrameWorkElement>" lists.

这是我填充 List"<"T> (SelectedObjects 和 CopyObjectsCollections 是 List"<"FrameWorkElement>" 列表的代码。

            foreach(FrameworkElement feObject in MainWindow.Data.SelectedObjects)
            {
                MainWindow.Data.CopyObjectsCollection.Add(feObject);
            }

i've looked at CollectionViewSource and IComparer but i can't really make any sense of it.

我看过 CollectionViewSource 和 IComparer ,但我真的无法理解它。

采纳答案by Sheridan

I might have miss-read your question, but if you just want to sort your List<T>, then why don't you just use the LinQOrderBymethod?

我可能没读懂你的问题,但如果你只是想对你的 进行排序List<T>,那你为什么不直接使用这个LinQOrderBy方法呢?

MainWindow.Data.CopyObjectsCollection = 
    MainWindow.Data.CopyObjectsCollection.OrderBy(f => f.Uid).ToList();

If that sorts it the wrong way round for your requirements, then you can use this:

如果按照您的要求对它进行了错误的排序,那么您可以使用它:

MainWindow.Data.CopyObjectsCollection = 
    MainWindow.Data.CopyObjectsCollection.OrderByDescending(f => f.Uid).ToList();

UPDATE >>>

更新 >>>

OrderByis a LinQextension method. Add using System.Linq;at the top of your class to use it. frelates to an instance of your FrameworkElementobject. The above lambda expression basically means 'sort using the Uidproperty values'.

OrderBy是一种LinQ扩展方法。添加using System.Linq;在您班级的顶部以使用它。fFrameworkElement对象的实例相关。上面的 lambda 表达式基本上意味着“使用Uid属性值排序”。

UPDATE 2 >>>

更新 2 >>>

The OrderBymethod does notalter the original collection... that is why my example sets the collection to the result of the OrderBymethod. See this basic example:

OrderBy方法并没有改变原始集合...这就是为什么我的例子中,收集组的结果OrderBy的方法。请参阅此基本示例:

List<FrameworkElement> elements = new List<FrameworkElement>();
elements.Add(new FrameworkElement() { Uid = "Object1003-1" });
elements.Add(new FrameworkElement() { Uid = "Object1002-2" });
elements.Add(new FrameworkElement() { Uid = "Object1002-1" });
elements.Add(new FrameworkElement() { Uid = "Object1001-1" });
elements.Add(new FrameworkElement() { Uid = "Object1001-3" });
elements.Add(new FrameworkElement() { Uid = "Object1001-2" });
string result = string.Join(", ", elements.Select(f => f.Uid));
elements = elements.OrderBy(f => f.Uid).ToList();
string orderedResult = string.Join(", ", elements.Select(f => f.Uid));

By comparing the values of resultand orderedResultyou can see that this orders them perfectly.

通过比较的价值观resultorderedResult你可以看到,这令他们完美。

UPDATE 3 (and hopefully the LAST one) >>>

更新 3(希望是最后一个)>>>

Dude, you need to learn about Lambda expressions... take a look at the Lambda Expressions (C# Programming Guide)page at MSDN for more information.

伙计,您需要了解 Lambda 表达式...查看MSDN上的Lambda 表达式(C# 编程指南)页面以获取更多信息。

elements = elements.OrderBy(f => f.Uid).ToList();

The fin this Lambda expression is declared inthis expression before the '=>'. It is fairly standard to name these parameters with one letter like Exceptions, but we couldname it anything:

f这个Lambda表达式声明的“在此之前的表达=>”。用一个字母来命名这些参数是相当标准的Exceptions,但我们可以任意命名:

elements = elements.OrderBy(frameworkElement => frameworkElement.Uid).ToList();