15,919,434 members
1.00/5 (1 vote)
See more:
Hi,
unfortunately im sitting at this problem for 4hours now, and it drives me crazy:

i got an 2 vectors as a members of my class.
the firstvector contains structs < with cstring Name, enum Kind and float transformation-factor>
and the vector with the sorting informations is filled by Enums for "Name"(0), "Kind"(1) and "transformation-factor"(2).
The Position of the Enum in the Vector tells us the Sort Order.
My Goal is to display the first vector data in a data grid, sorted by the users order.

Now i have problems to write a working functor, which shall tell the std::sort how to sort.

What I have tried:

The sorting functions should get the parameters and should stop the sortion as soon theres is a first matching pair, what is NOT l. == r.,
right now i not even can get the struct for the functor down in the .header,
also i have problems with the call of the functions in the .cpp, cause every step depends on the sorting-vector.
Who can help me and give me an example?
Posted
Updated 21-Jun-16 4:37am
[no name] 20-Jun-16 11:30am
How is about to let your original data like it is and introduce a third vector managing "only" the sort indices?
Sergey Alexandrovich Kryukov 20-Jun-16 11:43am
Not clear. What you want to achieve cannot be called "sorting", it it should stop before the end of sorting. No matter what it is, you should strictly formulate it. "l. == r." is not clear; you never introduced l or r, and this is not C++ code.
Probably you need to provide the code where you define vectors and the element types and clearly formulate the problem.
What have you tried so far?
—SA
Patrice T 20-Jun-16 12:21pm
Show a sample of your data hexibiting the situation.

## Solution 1

To achieve your own sort order you must overwrite the sorting algorithm, by overwriting the comparison operator "<" which is repeatly called in a sort call.

For a deeper inside read and understand the great article STL Sort Comparison Function.

Tip: make a small sample list and debug it to understand what is happening.

Dennis1990 5-Aug-16 8:49am
thank you KarstenK! your great article made it in my Literature List of my Thesis.
helped me a lot :)

## Solution 2

i used the STL Sort Comparision Funciton and could use the
Member < Operator
like it was described in the Article

next time ill use a functor :)

v2