sorting arrays

Hello. I made a simple program to collect data and print it but I have no idea how to sort my data in an alphabetic list (a-z) or by numbers(0-max number).
Can someone help me please. :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
using namespace std;
int main() {
	int n;
	int i;
	int end; 
	const int max = 10;
	string petName[max]; //array which holds strings
	int petAge[max]; // array which holds numbers
	for (n = 0; n < 10; n++) {
		cout << "Enter the pet's name ";
		cin >> petName[n];
		cout << "Enter the pet's age ";
		cin >> petAge[n];
		cout << "press 1 to exit else press 2 ";
		cin >> end;
		if (end == 1) { goto stop; }
	}
stop:
	for (i = 0; i < n + 1; i++) {
		cout << petName[i] <<" "<< petAge[i]<< endl;
	}
}
closed account (48T7M4Gy)
http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/bubble-sort/
Will this work with the string from a to z
closed account (48T7M4Gy)
Combined with this http://www.cplusplus.com/reference/string/string/compare/ the short answer is Yes

(PS This all assumes you are restricted to simple arrays and STL containers are a yet to be learned mystery at this stage. http://www.cplusplus.com/articles/NhA0RXSz/)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() 
{
    int z = 0;
    string name[] = {"john", "bobby", "dear", "test1", "catherine", "nomi", "shinta", "martin", "abe", "may", "zeno", "zack", "angeal", "gabby"};

    sort(begin(name),end(name));

    for(auto n: name){
         cout << n << endl;
    }
    return 0;    
}
Thanks to Stackoverflow http://stackoverflow.com/questions/18292619/c-string-array-sorting
Last edited on
This worked .
Thank you
Topic archived. No new replies allowed.