Qt / C ++ Sort Algorithm - Sort QList Structures

I wonder if their algorithm is in stl or in Qt, which sorts the double array and returns the indices of the sorted elements in the original list. For instance. L = 1, 2, 5, 3 L_sort = 1, 2, 3, 5 Indexes = 1, 2, 4, 3

So that I can subsequently compute AnotherList [Indexes] (the same order prevails in both lists with respect to the original list L).

In the end, I thought about creating a QList, each MyStruct containing two members, one LType type of the same type as the elements in L, the other of the same type as AnotherType as elements in AnotherList. And then we order in relation to members like LType. However, I have this idea, I do not know how to act in Qt.

Thank you and welcome

+6
source share
1 answer

You can store data with indices in pairs ... First, sort by values, second sort by indices ...

QList<QPair<LType,int> > array; for (int i = 0; i < 100; i++) { LType x = ... array.append(qMakePair(x,i)); } // Ordering ascending qSort(array.begin(), array.end(), QPairFirstComparer()); ..... // Restoring start order qSort(array.begin(), array.end(), QPairSecondComparer()); 

You just need these classes:

 struct QPairFirstComparer { template<typename T1, typename T2> bool operator()(const QPair<T1,T2> & a, const QPair<T1,T2> & b) const { return a.first < b.first; } }; struct QPairSecondComparer { template<typename T1, typename T2> bool operator()(const QPair<T1,T2> & a, const QPair<T1,T2> & b) const { return a.second < b.second; } }; 
+9
source

Source: https://habr.com/ru/post/913406/


All Articles