I have strings like this:
var a = "ABCFE";
Is there a simple way that I can sort this string into:
ABCEF
Thanks
The main logic is to toCharArray() method of the String class over the input string to create a character array for the input string. Now use Arrays. sort(char c[]) method to sort character array. Use the String class constructor to create a sorted string from a char array.
Use sorted() and str. join() to sort a string alphabetically in Python. Another alternative is to use reduce() method. It applies a join function on the sorted list using the '+' operator.
You can use LINQ:
String.Concat(str.OrderBy(c => c))
If you want to remove duplicates, add .Distinct()
.
Yes; copy the string to a char array, sort the char array, then copy that back into a string.
static string SortString(string input)
{
char[] characters = input.ToArray();
Array.Sort(characters);
return new string(characters);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With