I have a table stored in contiguous memory. It needs to stay in that format after the sort.
For example:
int table[5][3] = {
{ 60, 5, 10 },
{ 0, 200, 15 },
{ 55, 50, 365 },
{ 4, 7, 78 },
{ 555, 8, 11 },
};
Except much bigger (the size of the biggest, in bytes, is approximately 27 KB). Each cell is always an int32, and all rows have the same amounts of columns.
Let’s say that I want to sort it based on the first column, so that the result must be equivalent to:
{ 0, 200, 15 },
{ 4, 7, 78 },
{ 55, 50, 365 },
{ 60, 5, 10 },
{ 555, 8, 11 },
What’s the best way to do this? I imagine there is a better way than to convert this to a std::list, call sort(), and convert back.
Also, something built into C++ where I just have to call some function would be best.
std::sortwon’t do it easily because arrays aren’t assignable.However,
std::qsortwill do it:OK, so
std::qsortdoesn’t benefit from template inlining optimization, but it gets the job done and at least you aren’t allocating a lot of memory and doing unnecessary copying.You could instead look to replace your array of
int[3]with an array of structs with anint[3]as a data member. That would then be assignable and you could usestd::sortnormally. Depends how much other code you have written that relies on the current type, and whether it’s OK to break the interface that code uses.