c++ - How to define a custom compare function to sort a matrix according as one dimensional array sort -
i'm trying sort 2 dimensional array (matrix) while 1 dimensional array sorted relevant row order.
how can define suitable compare function?
(or. should have code own retro style bubble sort function)
double matrix[4][3]; double id[4]; fillallarrays();//declared somewhere std::sort(std::begin(matrix),std::end(matrix),compare); //how can define compare function ?
the following demonstration displays input 2 arrays (before) , want them sorted in section (after) sort id array values, relevant rows of matrix should identically reorder.
(thanks in advance response , ideas)
before
double matrix[4][3] 0.45 0.67 0.41 0.94 0.34 0.34 0.12 0.50 0.42 0.34 0.52 0.74 double id[4] 35 67 12 47
after
double matrix[4][3] 0.12 0.50 0.42 0.45 0.67 0.41 0.34 0.52 0.74 0.94 0.34 0.34 double id[4] 12 35 47 67
moving rows of matrix around during sorting should avoid. instead sort vector of
struct idandindex{ double id; int index; };
that contains id
, original index in array. once sorted std::vector<idandindex>
can rearrange matrix rows accordingly.
or if want sort matrix directly (maybe small) instead sort vector of
struct idandrow { double id; double[3] row; bool operator<(const idandrow& other) { return id < other.id; } };
Comments
Post a Comment