Fraiser Fraiser - 25 days ago 6
C# Question

Lexicographical sort algorithm using C#

I am struggling to write a sorting algorithm that can sort characters in a word lexicographically (alphabetically) as follows

lexicographical sort of the word :- Contamination


lexicographically

Sorted Text               Index

------------------         ----------

amination                      0

ation                              1

contamination                2

ination                            3

ion                                  4

mination                          5

n                                    6

nation                            7

ntamination                    8

on                                  9

ontamination                  10

tamination                       11

tion                                12



Could anyone please help write a pseudo code / or an implementation in C# or VB.NET of how I can do a lexicographical sort of the word above?

Answer
string str = "contamination";
IEnumerable<string> sortedSubstrings =
    Enumerable.Range(0, str.Length)
              .Select(i => str.Substring(i))
              .OrderBy(s => s);