Sorting String

1
2
3
4
5
6
7
8
9
10
11
void sortMovieTitle(Movie topMovies[]){
	string name = topMovies[0].title;
	for(int x = 0; x < SIZE; x++){
		if(name > topMovies[x+1].title){
			topMovies[x] = topMovies[x+1];
			topMovies[x+1].title = name;
		}

	}
	
}


I need to sort a string by alphabetical order... what im I doing wrong... T_T please help
For a bubblesort you need two nested loops. To swap elements you need a temporary variable.
https://www.tutorialspoint.com/data_structures_algorithms/bubble_sort_algorithm.htm

Much easier would be to use std::sort.
http://www.cplusplus.com/reference/algorithm/sort/
I tried using the sort but i get an error saying that algorithm line 3255 has a problem or something close to that line...
What is the error message?
<algorithms> before so im kinda new to it..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void sortMovieTitle(Movie topMovies[]){
	string name = topMovies[0].title;
	bool swapped;
	for(int x = 0; x < SIZE - 1; x++){
		swapped = false;
		for(int y = 0; y < SIZE - 1; y++){
			if(topMovies[x].title > topMovies[x+1].title){
				swap(topMovies[x].title, topMovies[x+1].title);
				swapped = true;
			}
			if(swapped){
				break;
			}
		}
	}
	
}


It somewhat works, but its not comparing characters or strings....
Last edited on
Topic archived. No new replies allowed.