Help with sorting an array of strings

I'm having trouble sorting an array. I need to sort an array of five names alphabetically but am not sure on the best way of going about this, I've only ever sorted integer arrays any help would be greatly appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>
using namespace::std;

//  function will display on cout the contents of an arrary -  FUNCTION 1
void printArray( ostream & out, const string data[], int cellsUsed);


//  function will sort an array                             -  FUNCTION 2
void sortArray( string data[], int cellsUsed);



void main()
{
	const int CELLS = 5;
	string  names[CELLS] = { "tom", "mary", "ann", "bill","carol"};
    
	cout << "Original array" << endl;
	printArray(cout,names,CELLS);
	cout << endl;

	sortArray(names,CELLS);

	cout << "Sorted array" << endl;
	printArray(cout,names,CELLS);
	cout << endl;

}   // end main


//  write FUNCTION 1
void printArray( ostream & out, const string data[], int cellsUsed)
{
	for (int i=0; i <cellsUsed;i++)
		out << data[i] << endl;
}


//  write FUNCTION 2
void sortArray( string data[], int cellsUsed)
{
	for (int i=0; i <cellsUsed;i++)
	{
		//??????????????????????
	}
}
can you use buble sort ?

http://mathbits.com/MathBits/CompSci/Arrays/Bubble.htm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void BubbleSort(apvector <int> &num)
{
      int i, j, flag = 1;    // set flag to 1 to start first pass
      int temp;             // holding variable
      int numLength = num.length( ); 
      for(i = 1; (i <= numLength) && flag; i++)
     {
          flag = 0;
          for (j=0; j < (numLength -1); j++)
         {
               if (num[j+1] > num[j])      // ascending order simply changes to <
              { 
                    temp = num[j];             // swap elements
                    num[j] = num[j+1];
                    num[j+1] = temp;
                    flag = 1;               // indicates that a swap occurred.
               }
          }
     }
     return;   //arrays are passed to functions by address; nothing is returned
}


use your mind
While I see how this would work with numbers I'm not sure on the syntax for comparing the first character in a string, I'll keep looking in my book. Thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <algorithm>

int main()
{
	const int CELLS = 5;
	std::string  names[CELLS] = { "tom", "mary", "ann", "bill", "carol" };
	std::sort(names, names + CELLS);

	for (auto element : names)
		std::cout << element << ' ';

	std::cin.ignore(10000, '\n');
	return 0;
}
Wow, that totally works even though I have no idea how... I've never used #include <algorithm> or auto before.

I was trying to do something like this but it crashes everytime it runs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void sortArray( string data[], int cellsUsed)
{
	for (int i=0; i <cellsUsed;i++)
	{
		
		if (data[i+1][0] > data[i][0])
		{
			char temp = data[i][0];             // swap elements
            data[i][0] = data[i+1][0];
            data[i+1][0] = temp;
		}
		
	}
}
Ok, so I finally got my code sorted out! Thanks Yanson, that include algorithm thing is amazing!

If anyone's interested here it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//  CIS 235  Exercise 1  

#include <iostream>
#include <string>
#include <algorithm>
using namespace::std;

//  function will display on cout the contents of an arrary -  FUNCTION 1
void printArray( ostream & out, const string data[], int cellsUsed);


//  function will sort an array                             -  FUNCTION 2
void sortArray( string data[], int cellsUsed);



void main()
{
	const int CELLS = 5;
	string  names[CELLS] = { "tom", "mary", "ann", "bill","carol"};
    
	cout << "Original array" << endl;
	printArray(cout,names,CELLS);
	cout << endl;

	sortArray(names,CELLS);

	cout << "Sorted array" << endl;
	printArray(cout,names,CELLS);
	cout << endl;

	if (names[0][0] >  names[1][0])
		cout << "YAY" << endl;
}   // end main


//  write FUNCTION 1
void printArray( ostream & out, const string data[], int cellsUsed)
{
	for (int i=0; i <cellsUsed;i++)
	{
		out << data[i] << endl;
	}
}


//  write FUNCTION 2
void sortArray( string data[], int cellsUsed)
{
	sort(data, data+ cellsUsed);

}
Topic archived. No new replies allowed.