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 t开发者_Go百科he 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.
精彩评论