Вы можете использовать быструю сортировку :
static int partition(string[] arr, int start, int end) {
int pivot = end;
int i = start, j = end;
string temp;
while (i < j) {
while (i < end && string.Compare(arr[i], arr[pivot]) < 0)
i++;
while (j > start && string.Compare(arr[j], arr[pivot]) > 0)
j--;
if (i < j) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[pivot];
arr[pivot] = arr[j];
arr[j] = temp;
return j;
}
static void quicksort(string[] arr, int start, int end) {
if (start < end) {
int pivotIndex = partition(arr, start, end);
quicksort(arr, start, pivotIndex - 1);
quicksort(arr, pivotIndex + 1, end);
}
}
static void Main(string[] args) {
string[] arr = { "Hello", "Awesome", "Cool", "Stuff" };
quicksort(arr, 0, arr.Length - 1);
foreach (string s in arr) {
Console.Write(s + " ");
}
Console.ReadKey();
}
Источник: http://www.bluesharktutorials.com/2013/08/quick-sort-algorithm-with-c-sharp-program-code.html. Я изменил int
на string
.
http://stackoverflow.com/questions/6965337/sort-a-list-alphabetically – Sajeetharan
Возможный дубликат [String «Сортировка шаблона» на C#] (http://stackoverflow.com/questions/379234/string- sort-template-in-c-sharp) –
Quicksort? http://www.softwareandfinance.com/CSharp/QuickSort_Recursive.html – romanoza