Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Sort the words in lexicographical order in C#
Lexicographical order means sorting strings alphabetically, similar to how words are arranged in a dictionary. In C#, there are multiple ways to sort an array of strings in lexicographical order using built-in methods and LINQ.
Syntax
Using LINQ query syntax −
var sorted = from word in array
orderby word
select word;
Using Array.Sort() method −
Array.Sort(array);
Using LINQ OrderBy() method −
var sorted = array.OrderBy(word => word);
Using LINQ Query Syntax
The most readable approach uses LINQ query syntax with orderby clause −
using System;
using System.Linq;
class Program {
static void Main() {
string[] arr = new string[] {
"Indian",
"Moroccon",
"American",
"Chinese",
"Brazilian"
};
var sort = from a in arr
orderby a
select a;
Console.WriteLine("Original array:");
foreach(string word in arr) {
Console.WriteLine(word);
}
Console.WriteLine("\nSorted in lexicographical order:");
foreach(string res in sort) {
Console.WriteLine(res);
}
}
}
The output of the above code is −
Original array: Indian Moroccon American Chinese Brazilian Sorted in lexicographical order: American Brazilian Chinese Indian Moroccon
Using Array.Sort() Method
The Array.Sort() method sorts the array in-place, modifying the original array −
using System;
class Program {
static void Main() {
string[] countries = new string[] {
"Germany", "France", "Italy", "Spain", "Belgium"
};
Console.WriteLine("Before sorting:");
foreach(string country in countries) {
Console.WriteLine(country);
}
Array.Sort(countries);
Console.WriteLine("\nAfter sorting:");
foreach(string country in countries) {
Console.WriteLine(country);
}
}
}
The output of the above code is −
Before sorting: Germany France Italy Spain Belgium After sorting: Belgium France Germany Italy Spain
Using LINQ OrderBy() Method
The OrderBy() method returns a new sorted sequence without modifying the original array −
using System;
using System.Linq;
class Program {
static void Main() {
string[] colors = new string[] {
"Red", "Blue", "Green", "Yellow", "Purple", "Orange"
};
var sortedColors = colors.OrderBy(color => color);
Console.WriteLine("Sorted colors:");
foreach(string color in sortedColors) {
Console.WriteLine(color);
}
Console.WriteLine("\nOriginal array unchanged:");
foreach(string color in colors) {
Console.WriteLine(color);
}
}
}
The output of the above code is −
Sorted colors: Blue Green Orange Purple Red Yellow Original array unchanged: Red Blue Green Yellow Purple Orange
Comparison
| Method | Modifies Original | Performance | Use Case |
|---|---|---|---|
| Array.Sort() | Yes | Fastest | When you want to sort in-place |
| LINQ OrderBy() | No | Good | When you need a new sorted sequence |
| LINQ Query Syntax | No | Good | When readability is important |
Conclusion
C# provides multiple ways to sort strings in lexicographical order: Array.Sort() for in-place sorting, and LINQ methods like OrderBy() and query syntax for creating new sorted sequences. Choose the method based on whether you need to preserve the original array and your performance requirements.
