# How to sort the data???

• 08-24-2011
justin8077
How to sort the data???
Here is my code...
Code:

```void Show_Ranking(float Time_Diff[], int Attempts[], int No_Participants, char Name_Participants[][128]) {         int smallest_value;         int start_value;         int current_value;         int ctrl;         for(start_value=0; start_value<No_Participants; start_value++)         {                 smallest_value=start_value;                 for(current_value=start_value+1; current_value<No_Participants; current_value++)                 {                         if(Attempts[current_value] < Attempts[smallest_value])                         {                                 smallest_value=start_value;                         }                         else                                 if(Attempts[current_value] == Attempts[smallest_value] &&                                         Time_Diff[current_value]<Time_Diff[smallest_value])                                 {                                         smallest_value=start_value;                                 }                 }                 swap(Attempts[start_value],Attempts[smallest_value]);                 swap(Name_Participants[start_value],Name_Participants[smallest_value]);         }                 for(ctrl=0; ctrl<No_Participants; ctrl++)                 {                         cout << ctrl+1 << "\t" << Name_Participants[ctrl];                         cout << endl;                 } }```
This is a multi player mastermind game, now i'm stuck with how to sort the participant with their attempts?? And if their attempts is same, it will sort with respect to the time??
• 08-24-2011
AndrewHunter
1. Why are you using arrays? This is C++, you should look into the STL. Specifically I would look at std::vector and then bust out std::sort. Here is a quick example, however you would drop the array all together.
Code:

```#include <iostream> #include <vector> #include <algorithm> #include <iterator> int main() {     //this would be user input     int userints[] = {5, 3, 40, 25, 6, 1, 34};     std::vector<int> myVector(userints,                               userints + sizeof(userints) / sizeof(userints[0]));     std::cout << "Before sort:\n";     std::copy(myVector.begin(), myVector.end(),               std::ostream_iterator<int>(std::cout, " "));     std::sort(myVector.begin(),myVector.end());     std::cout<<"\nAfter sort:\n";     std::copy(myVector.begin(), myVector.end(),               std::ostream_iterator<int>(std::cout, " "));     std::cout << std::endl; }```
• 08-24-2011
rogster001
Perhaps consider a map, and the maybe the line
Code:

`using namespace std;`
With the map you can have an associative collection,
Code:

`map<string, int> player;`
so that would be a list of names associated with a score, you won't get the ::sort function but it's no disaster here, there are regular sorts you could write just relying on string comparisons.

Not sure if the map is the very best approach, but it strikes me as of interest in this scenario.
• 08-24-2011
tabstop
sort is a good start, but you'll still need to write your own comparator to sort on multiple criteria. Step 1 would seem to be to recognize that <name, attempts, time> is one "thing" and create a datatype to hold that thing.

To actually write your "BetterThan" function, you would first see if the attempts are different -- if they are you can return true/false based on the attempts. If the attempts are the same, you can then check the time afterwards. (EDIT: And actually this is what you've got written, which means you just need to actually do all the entries and not forget about swapping time, which is why you make your datatype so that everything changes together.)
• 08-24-2011
justin8077
Quote:

Originally Posted by AndrewHunter
1. Why are you using arrays? This is C++, you should look into the STL. Specifically I would look at std::vector and then bust out std::sort. Here is a quick example, however you would drop the array all together.
Code:

```#include <iostream> #include <vector> #include <algorithm> #include <iterator> int main() {     //this would be user input     int userints[] = {5, 3, 40, 25, 6, 1, 34};     std::vector<int> myVector(userints,                               userints + sizeof(userints) / sizeof(userints[0]));     std::cout << "Before sort:\n";     std::copy(myVector.begin(), myVector.end(),               std::ostream_iterator<int>(std::cout, " "));     std::sort(myVector.begin(),myVector.end());     std::cout<<"\nAfter sort:\n";     std::copy(myVector.begin(), myVector.end(),               std::ostream_iterator<int>(std::cout, " "));     std::cout << std::endl; }```

I'm using array because this is what I'm told to do...
And I'm still new to C++
• 08-24-2011
AndrewHunter
Quote:

Originally Posted by justin8077
I'm using array because this is what I'm told to do...
And I'm still new to C++

This is a school project? It appeared that you were making a personal one; hmm at least this is an interesting project. Well, if you must stick with arrays then tabstop's advice is still valid. Take a look at combining <name,attempts,time> into one object and then make an array of those objects. From there call your sorting function.