Thecube
Thecube

Reputation: 71

Sorting an Array with corresponding arrays

Currently created a piece of code that is able to output from a text file, I am reading the text and the putting each different piece of information into an array.

I have used 4 different arrays, as there is 4 different types of information I want to store. The code for this is working as expected, but I am unsure how I can now sort the information in a way which 1 of the arrays is sorted alphabetically, with all the corresponding arrays to stay in line and be output at the correct time.

void displayfile(){

 string filename1;
 string rartist[NUM];
 string rtitle[NUM];
 string ryear[NUM];
 string rcategory[NUM];
 ifstream openFile;
 int counter = 0;
 int continu
 bool error = false;
 cout << "test";

 do{
     //Loop to retrieve the file name from user, then open file
     do{
        cout  << "Please enter the name of the menu you would like to open: ";
        cin >> filename1;
        filename1 +=  ".txt";
        openFile.open(filename1.c_str());
        if(openFile.fail()){
            cerr << "Check spelling of file name.\n";
            error = true;
        }
    //Storing text from file into arrays
    }while(error == true);
    while(getline( openFile, rartist[counter], ':') && getline( openFile, rtitle[counter], ':') &&
      getline( openFile, ryear[counter], ':') && getline( openFile, rcategory[counter])){
    counter++;
    }
    //outputting the information stored in the array
    cout << "ARTIST   " << "  DVDTITLE    " << "    YEAR    " << "  CATEGORY    \n";
    for(int i = 0; i < counter; i++){
        cout << rartist[i] << "               " << rtitle[i] << "               "
             << ryear[i] << "              " << rcategory[i] << "\n";
    }
    cout << "\n\nIf you would like to read another file, Press 1: ";
    cin >> continu;
  }while(continu == 1)
}

This is the function that I am using to display the text currently.

Upvotes: 1

Views: 78

Answers (2)

Surt
Surt

Reputation: 16119

Totally untested c++11 code

std::vector<int> indexes(NUM);
// fill with 0..NUM-1
std::iota(indexes.begin(), indexes.end(), 0);

// example sort after artist.
std::sort(indexes.begin(), indexes.end(),
    [&rartist](const int &lhs, const int &rhs) { 
        return rartist[lhs] < rartist[rhs];
    });
// indexes is now sorted in the same way rartist would have been.

// now iterate in order.
for (int i : indexes) { 
    std::cout << rartist[i] << "              "
              << rtitle[i]  << "              "
              << ryear[i]   << "              "
              << rcategory[i] << "\n";
}

Upvotes: 1

Nelfeal
Nelfeal

Reputation: 13269

I assume you are reading pieces of information about songs, and you want to sort them according to the song titles. Since you are reading the same kind of data for each song, use a single array of structures, not separate arrays.

For instance, this is how you would sort the songs by title.

struct Song {
    std::string artist,
    std::string title,
    std::string year,
    std::string category
};

std::vector<Song> songs(NUM);

// Read data

std::sort(songs.begin(), songs.end(),
    [](const Song &a, const Song &b) {
        return a.title < b.title;
    });

Upvotes: 3

Related Questions