sort an array of stings alphabetically

I need to sort an array of stings alphabetically. The trouble is I have no idea how to do that. I need it to be case insensitive. My professor reccomended using toupper() with he built in string comparison so essentially I should turn [jon, Sally, ReBBa] into [JON, SALLY, REBBA] then compart them, sort them and then somehow return [jon, ReBBa, Sally] but I'm not sure how to do that. Any help would be greatly appreciated.

This is the example from the book.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void selectionSort (int array[], int size)

{
    int startScan, minIndex, minValue;
    
    for (startScan=0; startScan<(size-1); startScan++)
    {
        minIndex = startScan;
        minValue = array[startScan];
        
        for (int index=startScan+1; index<size; index++)
        {
            if (array[index] < minValue)
            {
                minValue=array[index];
                minIndex=index;
            }
        }
        array[minIndex] = array[startScan];
        array[startScan] = minValue;
    }
}
That snippet works for an array of ints, now you need it to work for an array of strings.
Line 1: array[] needs to be of type string instead of int
Line 4: minValue should be removed from here, because the values are strings, not ints
after Line 4: declare a string called minValue to replace the one you just removed
Line 13: call toupper on each of the things you're comparing before you actually compare them: if (toupper(array[index]) < toupper(minValue))
You might start with a function to compare two strings and determine which comes first according to your requirements. Once you've done that it is a simple case of retooling your sort function to work with strings, the biggest part of which is using the comparison function you just created to replace the less-than comparison on line 13.
Topic archived. No new replies allowed.