Home
Manage Your Code
Snippet: Alphabetical Bubble Sort (C#)
Title: Alphabetical Bubble Sort Language: C#
Description: This will sort a string array alphabetically Views: 555
Author: J T Date Added: 9/1/2010
Copy Code  
1private string[] AlphabeticalBubbleSort(string[] values)
2        {
3            int i = 0;
4            int j = 0;
5            string temp;
6
7            for (i = values.GetUpperBound(0); i >= 0; i--)
8            {
9                for (j = 0; j < i; j++)
10                {
11                    if (string.Compare(values[j], values[j + 1], true) > 0) 
12                    {
13                        //Swap values
14                        temp = values[j].ToString();
15                        values[j] = values[j + 1];
16                        values[j + 1] = temp;
17                    }
18                }
19            }
20            return values;
21        }
Usage
string[] ListOfValues;
//Add values to array
ListOfValues = AlphabeticalBubbleSort(ListOfValues);