On Jan 4, 4:11 am, "(2b|!2b)==?" <void-s...@ursa-major.com> wrote:
I have a multi-dimentional array (for the purposes of this post - lets
assume the dimensionality = 2 (to keep things simple). The array
(implemented by vectors), consists of "columns", where each column is of
a particular data type (similar to a traditional database table).
I want to be able to sort the array by its columns (like in Excel or in
a databse). I need some pseudocode or code sippet that will show how I
can sort the table (i.e. vector of columns) by specified columns.
Ideally, the algorithm will be generic enough to apply to an
N-dimensional table.
The easiest thing to do is use std::sort:
http://www.sgi.com/tech/stl/sort.html
And supply it with a predicate function that compares the columns of
interest.
A general predicate for multiple fields is usually something like (say
you want to sort by field1, then field2, then ..., then fieldN):
bool isbefore (const Thing &a, const Thing &b) {
if (a.field1 < b.field1)
return true;
else if (b.field1 < a.field1)
return false;
else if (a.field2 < b.field2)
return true;
else if (b.field2 < a.field2)
return true;
// ... and so on ...
else
return a.fieldN < b.fieldN;
}
Jason
Hi Jason, could you please elaborate some more?. I mean, suppose I have