C++ Sorting problem

Hi! I am generally new to C++. I'm trying to create a phone book kind of program, and sort the input by phone number ascending. My problem is that after I get my numbers sorted, the names do not stay with their respective numbers. How would I sort the names so they stay with their numbers? Currently they are sorting numbers in order, and names alphabetically.

Here is the sorting portion of my code, everything before this is just output and populating the arrays name and number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
sort(number,number+size);

int i = 0;
int j = 0;
int min = 0;
const int size = 10;

for(i = 0; i < size-1; i++)
{
    min = i;
    for(j = i+1; j < size; j++)
    {
        if(name[j] < name[min])
        {
            string tempString = name[j];
            name[j] = name[i];
            name[i] = tempString;
        }
    }
}
so you sort off the number which you called name?

anyway, what exactly do you have, how are the names and numbers stored?

if you have 2 arrays, you just do to one what you do to the other:


name[j] = name[i]
number[j] = number[i] //do the same thing to both structures like this example!

If you have some other data structure, I need more info.



Here is my full program so far;
I want the input to be along the lines of
Bob Steve Joe Carl
22 57 1 20
After sort:
Joe Carl Bob Steve
1 20 22 57
But I'm getting
Bob Carl Joe Steve
1 20 22 57


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>
#include <cstdlib>
#include <iomanip>
#include <algorithm>
#include <iterator>
using namespace std;

main()
{
const int size = 10;
string name[10];//declaring an array name
int number[10];//declaring an array number

cout << "Please input 10 names \n";//output
for(int i = 0; i < 10; i++){//for statement
    cin >> name[i];//inputting names in the array names
}
cout << "Please input their corresponding numbers \n";//output
for(int i = 0; i < 10; i++){
    cin >> number[i];//inputting numbers
}//end for


int i;
int j;
int min;
int counter = 0;

for(i = 0; i < counter; i++)
{
    min = i;
    for(j = i+1; j < counter; j++)
    {
        if(name[j] < name[min])
        {
            string tempString = name[i];
            name[i] = name[j];
            name[j] = tempString;
        }
    }
}
for(i = 0; i < 10; i++)
{
    cout << name[i] << ": " << number[i] << "\n";
};
} //end main  
Last edited on
 My problem is that after I get my numbers sorted, the names do not stay with their respective numbers.

One way to ensure that names and numbers stay together is to couple them through a struct:
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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

struct PhoneBook
{
    std::string m_firstName;
    std::string m_lastName;
    double m_number;
    bool operator < (const PhoneBook& p)const
    {
        return m_lastName < p.m_lastName;
        //sorts PhoneBook instances by last name
    }
};
std::ostream& operator << (std::ostream& os, const PhoneBook& p)
//for printing
{
    os << p.m_firstName << " " << p.m_lastName << ": " << p.m_number << "\n";
    return os;
}

int main()
{
    std::vector<PhoneBook> book{{"John", "Smith", 123}, {"Jane", "Doe", 456}, {"An", "Other", 789}};
    //uniform initialization:https://msdn.microsoft.com/en-us/library/dn387583.aspx
    std::sort(book.begin(), book.end());//google this function if unfamiliar
    for (const auto& elem : book)std::cout << elem;
}
I haven't learned about vectors yet. Or structs. Fairly new to C++ and my book doesn't even have anything about vectors in it. Is there a way that I can keep the names with the numbers in parallel arrays and not a vector or pair?
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 <string>

int main() // *** int main
{
    const int size = 4;
    std::string name[size]; // declaring an array name
    int number[size] = {0}; // initialise to all zeroes

    std::cout << "Please input 10 names and their corresponding numbers\n" ;
    for( int i = 0; i < size; ++i )
    {
        std::cout << "name #" << i+1 << "? " ;
        std::cin >> name[i] ; // assumes that the name has no embeeded spaces
        std::cout << "number? " ;
        std::cin >> number[i] ; // we assume that the user enters a valid number
    } // end for

    // print the unsorted names and numbers
    std::cout << "\nbefore sort\n----------\n" ;
    for( int i = 0; i < size; ++i )
        std::cout << name[i] << ": " << number[i] << '\n' ;

    // sort on names: keep associated numbers in sync with the names
    // we assume that we haven't encountered functions as yet
    for( int i = 0; i < size; ++i )
    {
        for( int j = i+1; j < size; ++j )
        {
            if( name[j] < name[i] ) // if name[i] should appear before name [j]
            {
                // swap the names (could use std::swap for this)
                const std::string temp_str = name[i] ;
                name[i] = name[j] ;
                name[j] = temp_str ;

                // swap the corresponding numbers (could use std::swap for this too)
                const int temp_num = number[i] ;
                number[i] = number[j] ;
                number[j] = temp_num ;
            } // end if
        } // end inner for
    } // end outer for

    // print the sorted names and numbers
    std::cout << "\nsorted on name\n----------\n" ;
    for( int i = 0; i < size; ++i )
        std::cout << name[i] << ": " << number[i] << '\n' ;
} //end main 
@JLBorges OMG Many many thanks! That works perfectly. (and yes, I am just now learning of functions, but wanted to get more familiar with sorting before I tried splitting it all up. That is why my code only consisted of main)
Topic archived. No new replies allowed.