Sorting strings problem

I have been having major problems getting my Sort methods to work. I have scoured the cplusplus forms and could not find anything. I am using a Vector in a 'library' class:
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
#include "main.h"


using namespace std;

class MediaLibrary
{	
	private:
		std::vector<ID3tag> list;
		int size;

	public:

		MediaLibrary()
		{
			this->size = 0;
		}

		void import(vector<ID3tag> toSet, int s)
		{
			this->list = toSet;
			this->size = s;
		}

		
		//Adds a track to the end.
		void addTrack(ID3tag add) 
		{
			list.push_back(add);
			size++;
		}

		
		//Removes track by track number
		ID3tag removeTrack(int track) 
		{
			ID3tag out = list.at(track);
			list.erase(list.begin()+track);
			size--;
			return out;
			/*
			for(unsigned int i = 0; i < list.size(); i++)
			{
				if(track == list[i].getTrackNum())
				{
					list.erase(list.begin()+i);
					
				}
			}*/
		}

		
		//Modifies Track
		void modifyTrack(ID3tag track, int index) 
		{
			list.at(index) = track;
		}

		//Retrieves Track specified by position in vector
		ID3tag getTrack(int pos)
		{
			return list[pos];
		}


		//Retrieves size of List
		int length() 
		{
			return size;
		}
};


My problem is that my library won't sort. Here are the two methods I am using to sort:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void sortByYear(MediaLibrary &lib) {
	for(int i = 0; i < (int) lib.length(); i++) {
		for(int j = (int) lib.length(); j < i; j--) {
			if(lib.getTrack(i-1).getYear() < lib.getTrack(j).getYear()) {
				swap(lib.getTrack(i-1), lib.getTrack(j));
			}//Endif
		}//End For2
	}//End for1
}//End sortByYear

//Used Wikipedia as guideline for BubbleSort
void sortByTitle(MediaLibrary &lib) {
	for(int i = 0; i < (int) lib.length(); i++) {
		for(int j = lib.length(); j < i; j--) {
			if(lib.getTrack(i-1).getTitle() > lib.getTrack(j).getTitle()) {
				swap(lib.getTrack(i-1), lib.getTrack(j));
			}//Endif			
		}//End For2
	}//End for1
}//End sortByTitle 


If you need more code or information please let me know. This is for a school assignment.

getTrack() returns copies of the elements, so you are only swapping two temporaries.
For that to work, getTrack() must return a non-const reference.
How would you suggest I go about doing that?

I tried putting the following methods into the MediaLibrary and calling them instead of swap(). It did not work.

1
2
3
	void swapTracks(int i, int j) {
			swap(list[i], list[j]);
		}


and
1
2
3
4
5
	void swapTracks(int i, int j) {
			ID3tag temp = list[i];
			list[i] = list[j];
			list[j] = list[i];
		}


Is there anything else I should try?

NOTE: I just changed the method and added cout to check what values are going in, and it apparently isn't even getting to the loops!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//Used Wikipedia as guideline for BubbleSort
MediaLibrary sortByYear(MediaLibrary lib) {
	for(int i = 0; i < lib.length(); i++) {
		for(int j = lib.length(); j < i; j--) {
			const int one = lib.getTrack(i-1).getYear();
			const int two = lib.getTrack(j).getYear();
			cout<<"\nOne: "<<one;
			cout<<"\nTwo: "<<two;
			if(one > two) {
				cout<<"\ni-1"<<lib.getTrack(i-1).getYear();
				cout<<"\nj"<<lib.getTrack(j).getYear();
				lib.swapTracks((i-1), j);
				cout<<"\ni-1"<<lib.getTrack(i-1).getYear();
				cout<<"\nj"<<lib.getTrack(j).getYear();
			}//Endif
		}//End For2
	}//End for1
	return lib;
}//End sortByYear 


I have now gone from confused to baffled!

Last edited on
I fixed it. Turns out I accidentally had "i-1" when it should have been "j-1";

Everything works except my string sorting, but I will have that done in no time. Thanks for your help jsmith!
Topic archived. No new replies allowed.