sorting struct

1
2
3
4
5
6
struct a
{
int num1;
int num2;
};
a array[1000];


how can i sort this array on num1? should i make my own function or is there some easier way?
You could write the functor, use a lambda expression, use std::bind(), provide an operator< for your struct a.

What is not easy about writing a functor, though?
Here are two options that use std::sort:

Option 1: Create an external function. This is useful if the struct is part of a bigger library and you can't touch it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <algorithm>
struct a
{
    int num1;
    int num2;
};

bool acompare(a lhs, a rhs) { return lhs.num1 < rhs.num1; }

int main()
{
   a array[1000];
   std::sort(array, array+1000, acompare);
}


Option 2: Overload the < operator:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <algorithm>
struct a
{
    int num1;
    int num2;
    bool operator<(const a& rhs) const { num1 < rhs.num1; }
};

int main()
{
   a array[1000];
   std::sort(array, array+1000, acompare);
}
thanks Stewbond,
Option 1: Create an external function. This is useful if the struct is part of a bigger library and you can't touch it:

i chose this one, thanks very much again
Last edited on
a functor is an object that can be called like a function, with the function-call operator. All standard algorithms in C++ (sort(), max_element(), find_if(), accumulate(), etc) use them.
A pointer to the non-member function acompare in Stewbond's example is a primitive (because it's stateless) functor.
oh, ok, thanks Cubbi.
The scenario that Cubbi is mentioning would look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <algorithm>
struct a
{
    int num1;
    int num2;
    bool operator()(const a& lhs, const a& rhs) const { lhs.num1 < rhs.num1; }
};

int main()
{
   a array[1000];
   std::sort(array, array+1000, array[0]);
}
Also, in case you're curious about how to vary which member to sort by, here's a (possibly naive) method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <algorithm>
struct a
{
    static int sort_by;// determines which member to sort by
    int num1;
    int num2;
    bool operator<(const a& rhs) const
    {
        if( sort_by == 1) return num1 < rhs.num1;// sort by num1
        return num2 < rhs.num2;// sort by num2
    }
};
int a::sort_by = 1;// default will be to sort by num1

int main()
{
   a array[1000];
   std::sort(array, array+1000);// sorted by num1

   a::sort_by = 2;// changing sort criteria to num2
   std::sort(array, array+1000);// sorted by num2
}

I get this error when the 3rd argument array[0] is included in the call to sort():
c:\mingw\bin\..\lib\gcc\mingw32\4.5.2\include\c++\bits\stl_algo.h|2125|error: no match for call to '(a) (a&, a&)'|

Stewbond, are you sure that belongs?
It works great with just std::sort(array, array+1000)
Requires C++11:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <algorithm>

struct A
{
    int x, y;
};

int main()
{
    A array[1000];
    //...
    std::sort(array, array+1000, [](A a, A b){ return a.x < b.x; });
    std::sort(array, array+1000, [](A a, A b){ return a.y < b.y; });
}
http://ideone.com/Qk5GWY
Last edited on
Topic archived. No new replies allowed.