Originally Posted by
greatunknown
I have a class containing simple information for a music library such as artist name, album title, then a vector<string> for tracktitle, and vector<string> for tracknumber (using a string for tracknumber because it is being read in from a text file)...
How can I sort the album alphabetically by track title while keeping the tracknumber inline with its specific track?
I know how to sort the the tracktitles, but tracknumbers stay in numerical order...
Is there some reason you can't combine the track titles and track numbers into a single struct or class, so that they sort together, instead of using two parallel arrays?
Failing that, you'll need to modify the items in the track title array to include an index number:
Code:
class IndexedTrackTitle
{
public:
std::string trackTitle;
int index;
};
std::vector< IndexedTrackTitle > trackTitles;
Then, you initialize the indicies:
Code:
for( int n = 0; n < trackTitles.size(); ++n )
trackTitles[ n ].index = n;
Then sort the trackTitles array. Now, the indicies will tell you the corresponding track number for a given entry in the trackTitles array:
Code:
std::string number = trackNumbers[ trackTitles[ n ].index ]
Notice how it's absolutely necessary to store some kind of extra data with the track titles. If you're going to do that, you might as well just store the track number directly, and avoid this mess completely.