Reputation:
If I have a class like this
public class Character
{
public int speed = 0;
//Other code...
}
And another class which takes Characters for input, how can I sort the characters based on their speed?
I'm currently using an ArrayList to store the characters.
This is a mockup of what I'm looking for:
Before
character 1... speed 4
character 2... speed 9
character 3... speed 1
After
character 3... speed 1
character 1... speed 4
character 2... speed 9
Upvotes: 0
Views: 34
Reputation: 460208
You can use LINQ:
var orderedBySpeed = characters.OrderBy(c => c.Speed);
However, use a generic List<Character>
instead of an ArrayList
. If you insist on an ArrayList
(there's no reason to do so):
var orderedBySpeed = characters.Cast<Character>().OrderBy(c => c.speed);
Upvotes: 1