Sort the words in lexicographical order in C#


Firstly, set a string array −

string[] arr = new string[] {
   "Indian",
   "Moroccon",
   "American",
};

To sort the words in lexicographical order −

var sort = from a in arr
orderby a
select a;

Example

 Live Demo

Let us see the complete code −

using System;
using System.Linq;
class Program {
   static void Main() {

      string[] arr = new string[] {
         "Indian",
         "Moroccon",
         "American",
      };
      var sort = from a in arr
      orderby a
      select a;

      foreach(string res in sort) {
         Console.WriteLine(res);
      }
   }
}

output

American
Indian
Moroccon

Updated on: 22-Jun-2020

679 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements