Sort 2D array using c++'s stl::sort()

Hi guys,
If you have time, here's my question:
I'm trying to sort a 2D array A[m][2].
Just consider I have m nodes, A[i][0] is x-coordinate and A[i][1] is y-coordinate of Node i.
Basically, I'm sorting the "nodes" based on y-coordinate first, if they have the same y-coordinate then compare their x-coordinate.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//==============================
#include <iostream>
#include <algorithm>
using namespace std;

bool cmp2d(double* a, double* b){
  for(int i=1;i>=0;i--){
    if(a[i]>b[i])return true;
    else if(a[i]==b[i])continue;
    else return false;
  }
}
//-----------------------------------------------------------
int main(){
int m=5;
double nodeA[5][2]={{1,2.5},{2,0},{1.5,3},{2,2},{1,2}};

sort(&nodeA[0], &nodeA[m], cmp2d);
return 0;
}
//============================== 

It won't compile and I've no idea why........

Thank you very much for the help!
you are a bit off the mark here.

The parameters to the predicate function should be what you would get if you dereference
the pointer/iterator that you passed to the sort function (so for example if you were
to dereference &A[0] you would get double[2]).

Arrays are at the heart of the problem you are having - (at least in msvc) - the sort algorithm tries to make a temporary variable of the contents of the current pointer.
As we said earlier, the contents of your pointer is an array of doubles - so trying to create this temporary variable is like saying:

1
2
int array1 [3];
int temp[3] = array1 //this type of array operation is illegal  


That is why it won't compile.
Last edited on
Thank you very much, guestgulkan.
Do you think is there a way I can get around this and get the results I want?
If there isn't any easy solution to this,I guess I'll probably write my own sort().
Thanks a lot.
Don't use a 2d array. Instead, use a 1d array (probably wrapped in a class or something).
Will do. Thanks, firedraco!
Topic archived. No new replies allowed.