Sort the words in lexicographical order in C#

CsharpProgrammingServer Side Programming

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
raja
Published on 21-Aug-2018 13:08:10
Advertisements