Sorting a vector of structs

Hi,

in my C++ program i have a vector (lets name it "fooVector") which holds many elements of a stuct (lets name it "FooStruct").

1
2
3
4
5
6
7
8
9
struct FooStruct
{
	int Position;
	double ValueA;
	double ValueB;
	double ValueC;
};

vector<FooStruct> fooVector;


The FooStruct has an integer variable called "Position" and some other variables. Now i would like to sort my fooVector in order of the FooStruct->Position.

Maybe it is also important to know, that Position is not always unique, same positions could occur over several FooStructs.

Is this possible to do? Or do i have to use something other than a vector for such a use-case?

Please excuse my bad english.

Thanks for all replies in advance.

Regards,
Mathias
No, its easily possible. You can use algorithms std::sort, and pass it a functoid of what you want it to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <algorithm>
#include <vector>

// in some func
std::vector<FooStruct> fooVector;

// populate the vector

auto sortFunc = [](FooStruct a, FooStruct b) -> bool {
    return a.Position < b.Position;
};

std::sort(fooVector.begin(), fooVector.end(), sortFunc);
Last edited on
Wow, i didnt expect that this can be done with so less code.

Thank you NT3!
Last edited on
Or add an overloaded < operator to the struct or class.
1
2
3
4
5
6
7
8
9
10
11
12
struct FooStruct
{
    int Position;
    double ValueA;
    double ValueB;
    double ValueC;
	
    bool operator<(const FooStruct & rhs) const
    {
        return (Position < rhs.Position);
    }
};


then sorting is just:
 
    std::sort(fooVector.begin(), fooVector.end());

Ahh, thank you Chervil. Now it is clearer to me.

That is usually the way i do sorting in VB and C#. I didnt thought of operator overloading in C++.

or you can write a static compare (by position) method for FooStruct:

1
2
3
4
5
6
7
8
9
10
struct FooStruct
{
    int Position;
    double ValueA;
    double ValueB;
    double ValueC;
	
    static bool compareByPosition(const FooStruct& lhs, const FooStruct& rhs)
    { return lhs.Position < rhs.Position; }
};


std::sort(fooVector.begin(), fooVector.end(), FooStruct::compareByPosition);


this way you can write many comparers for each field in FooStruct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct FooStruct
{
    int Position;
    double ValueA;
    double ValueB;
    double ValueC;
	
    static bool compareByPosition(const FooStruct& lhs, const FooStruct& rhs)
    { return lhs.Position < rhs.Position; }
    static bool compareByValueA(const FooStruct& lhs, const FooStruct& rhs)
    { return lhs.ValueA < rhs.ValueA; }
    static bool compareByValueB(const FooStruct& lhs, const FooStruct& rhs)
    { return lhs.ValueB < rhs.ValueB; }
    static bool compareByValueC(const FooStruct& lhs, const FooStruct& rhs)
    { return lhs.ValueC < rhs.ValueC; }
};
Last edited on
There's some useful advice here. Real-world problems can require sorting on multiple fields, perhaps with some ascending, others descending. Flexible approaches to sorting are definitely useful.
Topic archived. No new replies allowed.