Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 358
Fraiser Avatar asked May 10 '26 16:05

Fraiser


1 Answers

You could simply do:

var parts = new List<string>();
for (int i = 0; i < word.Length; i++)
    parts.Add(word.Substring(i));

parts.Sort();

The output should be as you expect.

like image 105
Matthew Abbott Avatar answered May 13 '26 06:05

Matthew Abbott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!