Normally sorting Array of structure objects is easy. Consider an Array of a structure (AOS)
#define ITEMS 10
typedef struct MyStruct
{
char a;
int b;
}tMyStruct;
tMyStruct arr_mystruct[ITEMS];
I first fill this array of structure with values of < a , b> pairs.
If I now want to sort this array of structures according to the integer field, I can do it using libc qsort function by using a comparison function which takes two integer arguments.
Now consider that I replace the above structure in AOS format with SOA format
#define ITEMS 10
typedef struct MyStruct
{
char a[ITEMS];
int b[ITEMS];
}tMyStruct;
tMyStruct mystruct;
Now I can still use qsort to sort the array of integers b field, however this time I need to additionally sort a (array of characters) w.r.t sorting order of b.
So my question is what is a possible efficient way to do sorting for data laid out in SOA format instead of usual AOS format?
Can anyone help me with this ? Thanks!
a w.r.t sorting order of b. Can you please translate this one for me...aandbso that they are both sorted according tob's data. In other words, keep all the pairs of data together.std::sortonly works by means ofstd::lessandstd::swap, then you could solve your problem by providing a customstd::swapwhich takes care ofas as well. Otherwise there are really no "good" solutions.