C++ 自定义字符串排序
我正在使用以下命令对以下字符串集合执行排序: 使用的搜索词:“山” 结果:C++ 自定义字符串排序,c++,string,qt,sorting,C++,String,Qt,Sorting,我正在使用以下命令对以下字符串集合执行排序: 使用的搜索词:“山” 结果: 地精登山者 疯癫山 磁山 山 山山羊 山寨 山地泰坦 山谷 雪人山 雪山 山中淑女 顺序是根据词汇进行排序的。 最终,我希望订单有以下规则: 在顶部精确匹配 与前面按词汇排序的值完全匹配 包含在按词汇排序的单词中的匹配项 山(1) 山羊(2) 山寨(2) 山地泰坦(2) 山谷(2) 雪人山(2) 地精登山者(3) 疯癫山(3) 磁山(3) 雪山(3) 山上的女士(3) 有人知道这是如何实现的吗?我能够实现某种定制
- 地精登山者
- 疯癫山
- 磁山
- 山
- 山山羊
- 山寨
- 山地泰坦
- 山谷
- 雪人山
- 雪山
- 山中淑女
- 山(1)
- 山羊(2)
- 山寨(2)
- 山地泰坦(2)
- 山谷(2)
- 雪人山(2)
- 地精登山者(3)
- 疯癫山(3)
- 磁山(3)
- 雪山(3)
- 山上的女士(3)
bool CardDatabaseDisplayModel::lessThan(const QModelIndex &left, const QModelIndex &right) const {
QString leftString = sourceModel()->data(left).toString();
QString rightString = sourceModel()->data(right).toString();
if (leftString.compare(cardName, Qt::CaseInsensitive) == 0) {// exact match should be at top
return true;
}
if (rightString.compare(cardName, Qt::CaseInsensitive) == 0) {// exact match should be at top
return false;
}
return QString::localeAwareCompare(leftString, rightString) < 0;
}
bool-CardDatabaseDisplayModel::lessThan(常量QModelIndex&left,常量QModelIndex&right)常量{
QString leftString=sourceModel()->data(左).toString();
QString rightString=sourceModel()->数据(右).toString();
if(leftString.compare(cardName,Qt::CaseSensitive)==0){//最上面应该是精确匹配
返回true;
}
if(rightString.compare(cardName,Qt::CaseSensitive)==0){//最上面应该是精确匹配
返回false;
}
返回QString::localeAwareCompare(leftString,rightString)<0;
}
以下是一种完成当前代码的方法。它试图从最特殊的情况到最一般的情况进行分类:精确匹配、匹配加上内容,以及所有其他情况
bool CardDatabaseDisplayModel::lessThan(const QModelIndex &left,
const QModelIndex &right) const {
QString leftString = sourceModel()->data(left).toString();
QString rightString = sourceModel()->data(right).toString();
// The exact match (if any) should be at the top
if (leftString.compare(cardName, Qt::CaseInsensitive) == 0)
return true;
if (rightString.compare(cardName, Qt::CaseInsensitive) == 0)
return false;
// We know that neither is the perfect match.
// But is either a match-plus-some-stuff ?
bool isLeftType2 = leftString.startsWith(cardName, Qt::CaseInsensitive);
bool isRightType2 = rightString.startsWith(cardName, Qt::CaseInsensitive);
if (isLeftType2 && !isRightType2)
return true;
if (isRigthType2 && !isLeftType2)
return false;
// At this point we're sorting two matches of the same type
// Either both are matches-plus-some-stuff or partial matches
return QString::localeAwareCompare(leftString, rightString) < 0;
}
bool-CardDatabaseDisplayModel::lessThan(常量QModelIndex&left,
常数QModelIndex和右)常数{
QString leftString=sourceModel()->data(左).toString();
QString rightString=sourceModel()->数据(右).toString();
//精确匹配(如果有)应位于顶部
if(leftString.compare(cardName,Qt::CaseSensitive)==0)
返回true;
if(rightString.compare(cardName,Qt::CaseSensitive)==0)
返回false;
//我们知道两者都不是完美的匹配。
//但要么是火柴加上一些东西?
bool isLeftType2=leftString.startsWith(cardName,Qt::case不敏感);
bool isRightType2=rightString.startsWith(cardName,Qt::CaseSensitive);
如果(isLeftType2&!isRightType2)
返回true;
if(isRigthType2&!isLeftType2)
返回false;
//此时,我们正在对相同类型的两个匹配项进行排序
//要么两者都是匹配加上一些东西,要么是部分匹配
返回QString::localeAwareCompare(leftString,rightString)<0;
}
我假设像“登山者”这样的东西本身就是类型2而不是类型3,如果你不想的话,你可以在比较中添加一个
+”
。展示你所拥有的tried@ArunA.S添加了一些我作为实验尝试过的东西。