Ref T529, deterministic sort order when column values are equal

* the Qt model sorts by column
* when multiple values have the same column value the order among those is more or less random
* added additional property indexes to determine the order among the equal values
This commit is contained in:
Klaus Basan
2019-01-30 16:37:32 +01:00
committed by Mat Sutcliffe
parent 17f67d6106
commit 8c15f45007
8 changed files with 50 additions and 15 deletions

View File

@@ -193,19 +193,32 @@ namespace BlackGui
{
//! Sort with compare function
template<class ObjectType>
bool compareForModelSort(const ObjectType &a, const ObjectType &b, Qt::SortOrder order, const BlackMisc::CPropertyIndex &index, std::true_type)
bool compareForModelSort(const ObjectType &a, const ObjectType &b, Qt::SortOrder order, const BlackMisc::CPropertyIndex &index, const BlackMisc::CPropertyIndexList &tieBreakers, std::true_type)
{
const int c = a.comparePropertyByIndex(index, b);
if (c == 0) { return false; }
if (c == 0)
{
if (!tieBreakers.isEmpty())
{
const std::integral_constant<bool, true> marker;
return compareForModelSort<ObjectType>(a, b, order, tieBreakers.front(), tieBreakers.copyFrontRemoved(), marker);
}
return false;
}
return (order == Qt::AscendingOrder) ? (c < 0) : (c > 0);
}
//! Sort without compare function
template <typename ObjectType>
bool compareForModelSort(const ObjectType &a, const ObjectType &b, Qt::SortOrder order, const BlackMisc::CPropertyIndex &index, std::false_type)
bool compareForModelSort(const ObjectType &a, const ObjectType &b, Qt::SortOrder order, const BlackMisc::CPropertyIndex &index, const BlackMisc::CPropertyIndexList &tieBreakers, std::false_type)
{
const BlackMisc::CVariant aQv = a.propertyByIndex(index);
const BlackMisc::CVariant bQv = b.propertyByIndex(index);
if (!tieBreakers.isEmpty() && aQv == bQv)
{
const std::integral_constant<bool, false> marker;
return compareForModelSort<ObjectType>(a, b, order, tieBreakers.front(), tieBreakers.copyFrontRemoved(), marker);
}
return (order == Qt::AscendingOrder) ? (aQv < bQv) : (bQv < aQv);
}
} // namespace