sorting strings

How can I go about sorting an array of strings?
Am I having trouble because in a way a string is kind of like an array?

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
#include <iostream>
#include "bubbleSort.h"
#include <string>
using namespace std;

int main()
{
    int n=10;
    int arr[]={1,2,3,4,5,6,7,8,9,0};
    sort(arr,n);
    for(int i = 0; i < n; i++){
        cout<<arr[i]<<" ";
    }
    cout<<endl;
    double dArr[]={1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0};
    sort(dArr,n);
    for(int i = 0; i < n; i++){
        cout<<dArr[i]<<" ";
    }
    cout<<endl;
    double cArr[]={'a','b','c','d','e','f','g','h','i','j'};
    sort(dArr,n);
    for(int i = 0; i < n; i++){
        cout<<cArr[i]<<" ";
    }
    cout<<endl;
    string **sArr[]={"apple","donut","bot","iron","law","turtle","highlighter","giants","knicks","snare"};
    sort(sArr,n+1);
    for(int i = 0; i < n; i++){
        cout<<sArr[i]<<" ";
    }
    cout<<endl;


    return 0;
}

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
#include "bubbleSort.h"

void sort(int arr[], int n) {
	for (int last = n-1; last > 0; last--) {
		for (int i = 0; i < last; i++)
			if (arr[i] > arr[i+1])
				swap(arr[i], arr[i+1]);
	}
}
void sort(double arr[], int n) {
	for (int last = n-1; last > 0; last--) {
		for (int i = 0; i < last; i++)
			if (arr[i] > arr[i+1])
				swap(arr[i], arr[i+1]);
	}
}
void sort(string **arr[], int n) {
	for (int last = n-1; last > 0; last--) {
		for (int i = 0; i < last; i++)
			if (arr[i].size > arr[i+1].size)
				swap(arr[i], arr[i+1]);
	}
}
void sort(char *arr[], int n) {
	for (int last = n-1; last > 0; last--) {
		for (int i = 0; i < last; i++)
			if (arr[i] > arr[i+1])
				swap(arr[i], arr[i+1]);
	}
}

void swap(int &a, int &b) {
	int t = a;
	a = b;
	b = t;
}
void swap(double &a, double &b) {
	double t = a;
	a = b;
	b = t;
}
void swap(string &a, string &b) {
	string t = a;
	a = b;
	b = t;
}
void swap(char *a, char *b) {
	char *t = a;
	a = b;
	b = t;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef BUBBLE_SORT_H
#define BUBBLE_SORT_H

void sort(int arr[], int n);
void sort(double arr[], int n);
void sort(string **arr[],int n);
void sort(char *arr[], int n);
void swap(double &a, double &b);
void swap(int &a, int &b);
void swap(string **a, string **b)
void swap(char *a, char *b);

#endif
Last edited on
1
2
string **sArr[]={"
// WHY?↑↑ 
Pointer to a pointer? I thought it'd be useful because I want to sort an array of arrays, in a sense.
If you want an array of strings, then create an array of strings
string sArr[]={"apple","donut","bot","iron","law","turtle","highlighter","giants","knicks","snare"};
when I do that, the compiler tells me string wasn't declared in the scope, even though it was included in main. I included it in the .cpp and .h but I still get the same errors.
Topic archived. No new replies allowed.