Sort MethodΒΆ

Sorts elements in list in ascending order.

listName.Sort();

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
List<string> planets = new List<string>()
{
   "Mercury",
   "Venus",
   "Earth",
   "Mars",
   "Jupiter",
   "Saturn",
   "Uranus",
   "Neptune"
};

planets.Sort();

foreach(string planet in planets)
{
   Console.WriteLine(planet);
}

Output

Earth
Jupiter
Mars
Mercury
Neptune
Saturn
Uranus
Venus