Sorting Arrays

I can't seem to change this to descending from ascending, please help.

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  /*
*Author: Joe
*Date: * Created on June 1, 2018
*FileName: main.cpp
*Purpose: Simple Contact Manager.
*Input: User inputs names and phone numbers to arrays. User inputs name to search.
*Output: Program sorts and prints names and phone numbers according to the phone
*numbers acsending order. Also prints search query answer.
*Exceptions: None.
 */

/* 
 * File:   main.cpp
 * Author: JoePC
 */

#include <string>
#include <bits/stdc++.h>
using namespace std;
 
// Function to sort array name[] according to the order defined by phone[]

void pairsort(string name[], string phone[], int n)
{
    pair <string, string> pairt[n];
 
    // Storing the respective array
    // elements in pairs.
    for (int i = 0; i < n; i++) 
    {
        pairt[i].first = name[i];
        pairt[i].second = phone[i];
    }
 
    // Sorting the pair array.
    sort(pairt, pairt + n);
     
    // Modifying original arrays
    for (int i = 0; i < n; i++) 
    {
        name[i] = pairt[i].first;
        phone[i] = pairt[i].second;
    }
}
 
// Declaring main function.
int main()
{
    string name[3];
    string phone[3];
    int i;
    
    cout << "********************************************************" << endl;
    cout << endl;
    cout << "                     Contact Manager                    " << endl;
    cout << endl;
    cout << "********************************************************" << endl;
    cout << endl;
    
          for (int i = 0; i < 3; i++)//Starts loop
    {
        cout<<"Please Enter First Name of the Contact."<<endl;
        cout << endl;
        cin >> name[i];// Storing 3 names entered by user in an array 
        cout << endl;
        
        cout<<"Please Enter Phone Number with hyphens."<<endl;
        cout << "EXAMPLE: *555-555-5555*" << endl;
        cout << endl;
        cin >> phone[i];// Storing 3 phone #'s entered by user in an array
        cout << endl;
    } 
                          
    int n = sizeof(phone) / sizeof(phone[0]);
     
    // Sort Function calling
    pairsort(phone, name, n);
 
            for(i = 0; i < 3; i++)
            {
            cout << left << setw(7) << name[i]
                    << setw(20) << phone[i] << endl;
            }
    
         string searchValue = "";
    
         cout << endl;
     
    //Prompt user for search query.
         
    cout << "Please enter a name and press enter to search\n";
    cout << endl;
    cin >> searchValue;
    cout << endl;
    
    for(i = 0; i < 10; i++) {
        if(name[i]==searchValue) {
            
    break;
    }
    }
    if (i > 2) {
        cout << "The name " << searchValue << " was not found." << endl;  
    }
    else {
        cout << "The name " << searchValue << " was found and the number is "
                << phone[i] << endl;
    }
    
    return 0;
}
To sort it descending you need to call sort with a comparer.
http://www.cplusplus.com/reference/algorithm/sort/
Topic archived. No new replies allowed.