简体   繁体   中英

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. 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. 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#.

Thanks in advance.

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

Then set this sorted collection as datasource to 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. So use for example: List<Video> videos - this collection is filled from DB, XML, NetFlix webservice, whatever.

After filling this collection, bind it tu UI, in your case ListBox. Then, if you want to sort it, just use code i have posted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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