sorting while keeping attributes together

This is a discussion on sorting while keeping attributes together within the C++ Programming forums, part of the General Programming Boards category; I have a class containing simple information for a music library such as artist name, album title, then a vector<string> ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    13

    sorting while keeping attributes together

    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...

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by greatunknown View Post
    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.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    Great thanks,
    that seems like the most viable method, not sure if Ill be able to use the sort() function properly now, but whatever must be done must be done.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by greatunknown View Post
    Great thanks,
    that seems like the most viable method, not sure if Ill be able to use the sort() function properly now, but whatever must be done must be done.
    You'll have to implement operator<(), or write a standalone comparison functor.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. MURK - a small preview
    By Mario F. in forum Game Programming
    Replies: 27
    Last Post: 12-18-2006, 07:22 AM
  3. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  4. Unable to List Files Attributes on EXT2
    By Maragato in forum C Programming
    Replies: 16
    Last Post: 08-11-2004, 12:30 PM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21