简体   繁体   English

排序列表框 C#

[英]Sort ListBox C#

So I have a Class "Video" and The users enter the video name and rate different aspects of it using a numericupdown control.所以我有一个 Class “视频”,用户输入视频名称并使用 numericupdown 控件对其不同方面进行评分。 There is a button that the users click and the score for the video is calculated and the video, with it's score, and number ratings for each aspect, is added to a ListBox which just shows the name.有一个按钮可供用户单击,然后计算视频的分数,并将视频及其分数和每个方面的数字评级添加到仅显示名称的 ListBox 中。 So what I want to do, is have a button, that when clicked, sorts the list by checking the score each video got, sorting the list from highest score to lowest score.所以我想要做的是有一个按钮,当点击它时,通过检查每个视频获得的分数对列表进行排序,将列表从最高分数排序到最低分数。 I am using visual studio 2010 and coding in C#.我正在使用 Visual Studio 2010 并在 C# 中进行编码。

Thanks in advance.提前致谢。

videos = videos.OrderByDesc(x => x.Score).ToList();

Then set this sorted collection as datasource to ListBox.然后将此排序集合设置为 ListBox 的数据源。

Edit : to answer your question from comments.编辑:从评论中回答您的问题。

You should separate presentation from data, ie have some data structures to hold data, not storing them in UI.您应该将表示与数据分开,即有一些数据结构来保存数据,而不是将它们存储在 UI 中。 So use for example: List<Video> videos - this collection is filled from DB, XML, NetFlix webservice, whatever.因此,例如使用: List<Video> videos - 此集合由 DB、XML、NetFlix webservice 等填充。

After filling this collection, bind it tu UI, in your case ListBox.填充此集合后,将其绑定到 UI,在您的情况下为 ListBox。 Then, if you want to sort it, just use code i have posted.然后,如果你想对其进行排序,只需使用我发布的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM