Friday, 13 September 2013

C++ implementation of ORDER BY on struct

C++ implementation of ORDER BY on struct

i searched a lot here and on other sites as well but i have not found
something satisfying.
what i need is quite simple task - substantially to construct ORDER BY
operator in c++. this means i have struct with a number of various data
type members and i need a comparator for it with members and orderings
configurable. here is my pseudocode idea:
comparator.add(&MyStruct::member1, std::less);
comparator.add(&MyStruct::member2, std::greater);
std::sort(my_vector.begin(), my_vector.end(), comparator);
and i get data sorted by member1 and if it is equal member2 decides, and
so on.
i am not too good in stl and templates, but i can read and decipher some
code and found this as very appropriate solution:
http://stackoverflow.com/a/11167563
unfortunately in my work i have to use c++ builder with faulty 32bit
compiler that refuses to compile this correct code. it does support almost
nothing from c++11, it has boost 1.39 available.
does someone have any solution that could work for me with my resources
available? thank you in advance
EDIT:
i got very specialised solutions with hard-written comparison operators
which i am aware of and which do not work here too good. i missed this in
my question. my struct has at least 15 members and as i wrote, i need to
often change individual sort directions for members/columns (asc, desc).
too, i need to often change set of sorted members, just like in order by
operator in sql, for example. also i cannot use something like stable_sort
as i am just writing comparator for something like OnCompare event of some
class.

No comments:

Post a Comment