Hi,
I have created a vector of structs like this:
struct Review {
};
struct Review {
QString title;
QString page;
};
To copy to clipboard, switch view to plain text mode
I filled it with data from a database; now I need to sort alphabetically by title this vector; I made a bubbleSort function:
void MagazineWidget::bubbleSort(QVector<Review> vector)
{
bool swap = true;
for(int j = 0; swap; j++)
{
swap = false;
for(int k = vector.size()-1; k > j; k--)
{
if(vector.at(k).title < vector.at(k-1).title)
{
Review temp;
temp.title = vector.at(k).title;
temp.page = vector.at(k).page;
vector.at(k).title = vector.at(k-1).title;
vector.at(k).page = vector.at(k-1).page;
vector.at(k-1).title = temp.title;
vector.at(k-1).page = temp.page;
swap = true;
}
}
}
}
void MagazineWidget::bubbleSort(QVector<Review> vector)
{
bool swap = true;
for(int j = 0; swap; j++)
{
swap = false;
for(int k = vector.size()-1; k > j; k--)
{
if(vector.at(k).title < vector.at(k-1).title)
{
Review temp;
temp.title = vector.at(k).title;
temp.page = vector.at(k).page;
vector.at(k).title = vector.at(k-1).title;
vector.at(k).page = vector.at(k-1).page;
vector.at(k-1).title = temp.title;
vector.at(k-1).page = temp.page;
swap = true;
}
}
}
}
To copy to clipboard, switch view to plain text mode
and call it with this statement:
QVector<Review> reviews;
// ...
// Filling the vector
// ...
bubbleSort(reviews);
QVector<Review> reviews;
// ...
// Filling the vector
// ...
bubbleSort(reviews);
To copy to clipboard, switch view to plain text mode
During compilation I got the following error on lines 15-18:
error
: passing ‘
const QString’ as ‘
this’ argument of ‘QString
& QString::operator=(const QString
&)’ discards qualifiers
error: passing ‘const QString’ as ‘this’ argument of ‘QString& QString::operator=(const QString&)’ discards qualifiers
To copy to clipboard, switch view to plain text mode
Thanks
Bookmarks