.Sort()
The .Sort()
array method arranges the elements of an array in ascending or alphabetical order.
Syntax
Array.Sort(myArray);
.Sort()
is a static method of the Array
object. It takes one parameter, myArray
, a one-dimensional zero-based array.
Example
The following example initializes the letters
array with strings, then uses the .Sort()
method to arrange the strings alphabetically. Finally, a foreach
loop passes each element of the sorted array to the Console.Write()
method to be printed:
using System;public class Example{public static void Main(string[] args){string[] letters = {"M", "Y", "E", "T", "P"};Array.Sort(letters);foreach (string letter in letters){System.Console.Write(letter + " ");}}}
This example results in the following output:
E M P T Y
Codebyte Example
The following example is runnable and returns the numbers in the numbers
array in ascending order:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.