Question How to sort names (strings) with assigned numbers from greatest to least as 1st 2nd 3rd etc

Joined
Jul 7, 2022
Messages
7
Programming Experience
10+
Hello everyone. I am working on a score/grading app. Everything thing works fine except I am struggling with the actual functionality.
I am able to generate the scores as shown below in a listbox but unable to sort them by their value from the greatest to the least and assign the text '1st', '2nd', 'third' etc to the and 'draw' when the values of two or more persons are the same.
What I tried initially was to create a list, the transfer these items of the listbox into the list, sort it in reverse, but still doesnt help.
Seems like I need to split the names from the values and sort them from the highest to the least but if so, I find it difficult to assign the values back to the names accordingly after the sorting has been done and applying the grades 1st', '2nd', 'third', 'draw' etc to them.
I am sorry I couldnt present my effort here. I know LINQ may solve this but I am quite new to C#. I will be grateful for your help and assistance. I am prepared to learn. Thank you.

John: 30
Sarah: 50
David: 7
Joe: 12
Pam: 20
Peter: 53
James: 14
John: 12

DESIRE OUTPUT >>

Peter: 53 - 1st
Sarah: 50 - 2nd
John: 30 - 3rd
Pam: 20 - 4th
James: 14 - 5th
Joe: 12 - Draw
John: 12 - Draw
David: 7
 
As it stands, you would need to split the names from the numbers, which indicates that you shouldn't have mixed them in the first place. You should have the name and the number separate, then you can easily use each one independently. If you want to combine them for display then that's fine. What you should do is define a dedicated type for the data. It would have a Name property and a Score property, each of appropriate type. You could also override the ToString method to produce the combined output for display. You can then create a list of items of that type, sort that list as desired and bind it to the ListBox. You should get that part working correctly before you bother about the ranking part.
 
As it stands, you would need to split the names from the numbers, which indicates that you shouldn't have mixed them in the first place. You should have the name and the number separate, then you can easily use each one independently. If you want to combine them for display then that's fine. What you should do is define a dedicated type for the data. It would have a Name property and a Score property, each of appropriate type. You could also override the ToString method to produce the combined output for display. You can then create a list of items of that type, sort that list as desired and bind it to the ListBox. You should get that part working correctly before you bother about the ranking part.
Thanks Sir. Will give it a try right away.
 
As an aside, ListBox almost feels like the wrong UI control for displaying this information when there are multiple columns of "metadata" involved.
 

Latest posts

Back
Top Bottom