Sorting an array?

Hi guys, I need help sorting a multidimensional array. I am trying to write a code that lets a user put in several pieces of information and then uses a specific formula to come up with a rating. Basically, I want to use the final rating to rank the initial entry, which would be the name of a person or thing. I have gotten everything else done pretty much, the only thing I cant figure out is how to sort the final colum from highest to lowest.
To make things simple, lets say I had an array with 2 columns, 1 for a persons name and another for his age. I want to sort the 2nd column from oldest to youngest, and out put the names along with their age. Here is a simple sample code I made.

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
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
#include <functional>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    string x[10][10];
    int z, i;
    string w;
    
    cout << "How many people are there? ";
    getline (cin, w);
    stringstream(w) >> z;
    cout << " \n";

    for (i=0; i<z; i++)
    {
     cout << "What is his name? ";
     getline (cin, x[i][0]);

     cout << "How old is he? ";
     getline (cin, x[i][1]);
    }


    for (i=0; i<z; i++)
    {
     cout << x[i][0] << " is " << x[i][1] << " years old.\n";
    }
    
    return 0;
}


I've searched around for codes and algorithms to do the sorting, but they all were pretty long and looked kind of complicated. I am just starting out in C++, so I was wondering if there was a simple way of doing what seems to be a simple task. Any help would be appreciated.
Last edited on
Ick, sorting a MD array...I would suggest just putting the data you have (name/age) into a class or a struct and then sorting an array of that based on whatever the user wants.
I've tried converting my array code to a class code, but Im stuck now. I keep getting a compiler error and cant figure out what to do. First heres my code:

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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>
#include <functional>
#include <algorithm>
#include <vector>
using namespace std;


      class People
      {
        public:
        string name;
        float age;
      } ppl[100];

int main()
{
    int z, i;
    string w, x, t;
    
    cout << "How many people are there? ";
    getline (cin, w);
    stringstream(w) >> z;
    cout << " \n";

      for (i=0; i<z; i++)
      {
        
        cout << "What is his name? ";
        getline (cin, ppl[i].name);
        return ppl[i].name;    // error: cannot convert `std::string' to `int' in return 

        cout << "How old is he? ";
        getline (cin, x);
        stringstream(x) >> ppl[i].age;
        return ppl[i].age;    // error: [Warning] converting to `int' from `float' 
      }


    for (i=0; i<z; i++)
    {
      cout << ppl[i].name << " is " << ppl[i].age << " years old.\n";
    }
    
    return 0;
}


I listed the 2 errors next to the appropriate lines. Any ideas on how to fix this?
Nevermind guys, the fix was really simple. I just deleted the return lines and everything works. Now I just need to figure out how to sort the classes. Is qsort the best option?
If you are using #include <algorithm> you can use the sort from there.
I still cant figure out a way to sort the classes. Is there a sample sorting code that would work within the rest of the code that I wrote?
 
std::sort( &ppl[0], &ppl[z] );


You need to provide operator< for class People.
Thanks for all the help guys. Got everything to work on this small sample code. Now Im gonna go rewrite my main code using all this info. I might be back for some more help while I work on my main project. For future reference heres what the working code now looks like:

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
53
54
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>
#include <functional>
#include <algorithm>
#include <vector>
using namespace std;


      class People
      {
        public:
        string name;
        float age;
        bool People::operator<(const People& other) const
        {
         return (age > other.age);
        }

      } ppl[100];

int main()
{
    int z, i;
    string w, x, t;
    
    cout << "How many people are there? ";
    getline (cin, w);
    stringstream(w) >> z;
    cout << " \n";

      for (i=0; i<z; i++)
      {
        
        cout << "What is his name? ";
        getline (cin, ppl[i].name);

        cout << "How old is he? ";
        getline (cin, x);
        stringstream(x) >> ppl[i].age;
      }

    std::sort( &ppl[0], &ppl[z] );


    for (i=0; i<z; i++)
    {
      cout << ppl[i].name << " is " << ppl[i].age << " years old.\n";
    }
    
    return 0;
}
Hey guys, Im back. Now Im trying to figure out how to let the user decide on how to sort out the classes. First heres my code:

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>
#include <functional>
#include <algorithm>
#include <vector>
using namespace std;


      class People
      {
        public:
        string name, rank;
        float age, weight;
        bool People::operator<(const People& other) const
        {
         return (rank > other.rank);
        }

      } ppl[100];

int main()
{
    int z, i;
    string w, x, y, t;
    
    cout << "How many people are there? ";
    getline (cin, w);
    stringstream(w) >> z;
    cout << " \n";

      for (i=0; i<z; i++)
      {
        
        cout << "What is his name? ";
        getline (cin, ppl[i].name);

        cout << "How old is he? ";
        getline (cin, x);
        stringstream(x) >> ppl[i].age;

        cout << "How much does he weigh? ";
        getline (cin, y);
        stringstream(y) >> ppl[i].weight;
        
        cout << " \n";
      }

     cout << "Now, how do you want to rank the people, age or weight)? " << endl;
     getline (cin, t);
     cout << " \n";
     for (i=0; i<z; i++)
     {
       ppl[i].rank = t;
     }


    std::sort( &ppl[0], &ppl[z] );


    for (i=0; i<z; i++)
    {
      cout << ppl[i].name << " is " << ppl[i].age << " years old and weighs " << ppl[i].weight << " pounds. \n";
    }
    
    return 0;
}


This does not do anything, it doesnt end up sorting at all. I want the user to be able to choose whether to sort the people out, by age or weight in this instance. I cant figure out a way to get this done. Setting the user input to "rank" doesnt work, but if I put this

1
2
3
4
        bool People::operator<(const People& other) const
        {
         return (age > other.age);
        }


or this

1
2
3
4
        bool People::operator<(const People& other) const
        {
         return (weight > other.weight);
        }


into the code it works, but doesnt leave the user with a choice. Anyone have any ideas how I cant let the user set what the sorting order should be?
Here is another way to perform a sort by age.

1
2
3
4
5
6
struct SortByAge {
    bool operator()( const People& p1, const People& p2 ) const
         { return p1.age < p2.age; }
};

std::sort( &ppl[0], &ppl[z], SortByAge() );


Now you just have write a switch() statement on t and call std::sort()
with the right function object. (You'll need another function object to
sort by weight).
Topic archived. No new replies allowed.