David H David H - 2 months ago 6
C# Question

Is there a simple way that I can sort characters in a string in alphabetical order

I have strings like this:

var a = "ABCFE";


Is there a simple way that I can sort this string into:

ABCEF


Thanks

Answer

You can use LINQ:

String.Concat(str.OrderBy(c => c))

If you want to remove duplicates, add .Distinct().