Sorting array of type string, with strcmp

I am going round and round in circles with this and researched everywhere I'm at a wits end.

I have two strings

1
2
string amounts[i];
string names[i];


I want to sort the names alphabetically keeping each corresponding [i] value of each together.

I'm trying to use strcmp but am struggling completely with the code. I need to use pointers?
Use a normal loop swapping the values in names[] based on the results of strcmp(), but just also add another swap for the same values of amounts[].
Thank you for the reply.

I know how to code the swap I have done it for a numerical sort. I do not know how to code the strcmp function correctly.
Create a structure that contains the related information, overload the < operator, and call std::sort
1
2
3
4
5
6
7
8
struct A{
public:
  string amount, name;
  bool operator<(const A&b) const;
}

A array[size];
std::sort( array, array+size );
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct A{
            public:
            string amount, name;
            bool operator<(const A&b) const;
            };

    for(i=0;i<SIZE-1;i++)
    {
    A amounts[i];
    A names[i];
    }


    sort( names, names+SIZE );
    


This sorts the names list alphabetical, but how do i keep the amounts together with the names when printing to a file.

Alternatively is there a way to just use the regular array, its just we havn't covered structs yet.
but how do i keep the amounts together with the names when printing to a file.
They are together in the struct.
1
2
3
4
5
A array [size];
//...
//print
for( int K=0; K<size; K++)
  file << array[K].name << ' ' << array[K].amount << std::endl;

its just we havn't covered structs yet.
Maybe it is time for learn them. Do what Zhuge suggested. However then you need to implement you sort version.
Thanks for the reply --> I didn't mean to sound flippant in not wanting to learn structs its just im still having trouble with arrays etc!

Here is what I have so far with notes. Sorry if I am missing something totally obvious but I am having a little trouble understanding your code. Thanks for you patience.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct A{
            public:
            string amount, name;
            bool operator<(const A&b) const;
            } array[SIZE];



    for (int n=0; n<SIZE; n++)
    {array[n].amount=amounts[n]; // Fill the struct with the data from my arrays? Is this correct?
    array[n].name=names[n];}



   sort(array,array+SIZE); //This gives an error. Here I want to sort the NAMES ONLY and print their corresponding 'amount' together



    ofstream outfile; //Printing to file
    outfile.open ("outputalpha.txt");
    for( int K=0; K<SIZE; K++)
    outfile << array[K].amounts << ' ' << array[K].name << std::endl;
Last edited on
Your array is an array of struct A so you need to tell the sort function use which data member for sorting. Hence you need to provide a comparison function or functor to the sort.

http://www.cplusplus.com/reference/algorithm/sort/

Use the second version instead.

template <class RandomAccessIterator, class Compare>
void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );
1
2
3
4
for (int n=0; n<SIZE; n++){ 
    array[n].amount=amounts[n]; // Fill the struct with the data from my arrays? Is this correct?
    array[n].name=names[n];
}
The idea was to replace the two independent arrays, with the one from the struct.
However you could copy, sort the copy, and then return the values.

1
2
3
bool A::operator<(const A &b) const{
  return this->name < b.name;
}
But only if makes sense. (or use an external function, as sohguanh suggested)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    alphabetical:

{
    for(int i=0;i<SIZE-1;i++)
    {

        for( int j = 0; j < SIZE-1; j++ )
        {
        if( names[j].compare( names[j+1] ) > 0 )
            {
            swap (amounts[j], amounts[j+1]);
            swap (names[j], names[j+1]);
            }

        }
    }

}
Topic archived. No new replies allowed.