Home > Net >  Sorting vector of objects by object's variable
Sorting vector of objects by object's variable

Time:04-24

I have a vector of objects. Each of these objects has 2 fields (the values of which can be repeated), e.g:

//myClass name = myClass(x,y)
myClass obj1 = myClass(2,5);
myClass obj2 = myClass(2,4);
myClass obj3 = myClass(1,5);
myClass obj4 = myClass(3,2);
 
std::vector<myClass> myVector;
 
myVector.push_back(obj1);
myVector.push_back(obj2);
myVector.push_back(obj3);
myVector.push_back(obj4);

I want to sort the vector. First it should be sorted by 1st values. If the values of the 1st variable is the same, then should be sorted by second variable. Vector after sorting should be like that:

  1. obj3 //(1,5)
  2. obj2 //(2,4)
  3. obj1 //(2,5)
  4. obj4 //(3,2)

I have wrote this simple code with bubble sort:

for (int i = 0; i < myVector.size(); i  )
    {
        for (int j = 0; j < myVector.size() - 1; j  )
        {
            if (myVector[j].x < myVector[j   1].x)
                std::swap(myVector[j], myVector[j   1]);
        }
    }

Now myVector is sorted by first value, but how to sort elements, which first value it the same, by second value? Like in example?

CodePudding user response:

You can try something like that:

for (int i = 0; i < myVector.size(); i  )
    {
        for (int j = 0; j < myVector.size() - 1; j  )
        {
            if (myVector[j].x < myVector[j   1].x)
            {
                std::swap(myVector[j], myVector[j   1]);
            }
            else if (myVector[j].x == myVector[j   1].x)
            {
                if (myVector[j].y < myVector[j   1].y)
                    std::swap(myVector[j], myVector[j   1]);
            }
        }
    }

CodePudding user response:

You may change your if statement to:

if ( (myVector[j].x == myVector[j   1].x) ? (myVector[j].y < myVector[j   1].y) : (myVector[j].x < myVector[j   1].x)  ) 

CodePudding user response:

For starters if you want to sort your vector in the ascending order then at least you need to use the if statement the following way

        if (myVector[j 1].x < myVector[j].x)
            std::swap(myVector[j], myVector[j   1]);

instead of

        if (myVector[j].x < myVector[j   1].x)
            std::swap(myVector[j], myVector[j   1]);

A simple approach is to use the standard function std::tie declared in the header <tuple>.

For example

#include <tuple>

//...

for (int i = 0; i < myVector.size(); i  )
{
    for (int j = 0; j < myVector.size() - 1; j  )
    {
        if ( std::tie( myVector[j 1].x, myVector[j 1].y ) < 
             std::tie( myVector[j].x, myVector[j].y ) )
            std::swap(myVector[j], myVector[j   1]);
    }
}

Instead of the manually written loops you could use the standard algorithm std::sort. For example

#include <tuple>
#include <vector>
#include <iterator>
#include <algorithm>

//...

std::sort( std::begin( myVector ), std::end( myVector ),
           []( const auto &obj1, const auto &obj2 )
           {
               return std::tie( obj1.x, obj1.y ) < std::tie( obj2.x, obj2.y );
           } );
  • Related