3

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?

3 Answers 3

1

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]);
            }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

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)  ) 

4 Comments

You're right but i think it's little bit complicated to understand for a novice
@YannisSauzeau Yeah, this code works, but I really don't understand how...
look at my code above, you see that the statement std::swap(myVector[j], myVector[j + 1]); is the same on both conditions, the only difference is the statement if (myVector[j].x < myVector[j + 1].x) that becomes if (myVector[j].y < myVector[j + 1].y) when myVector[j].x == myVector[j + 1].x, so you can use ternary operator to avoid this code duplication. But it's better for you at the beginning to use comprehensive code with duplication, it's for that i propose to use you my code above.
@armin-montigny I don't find the ternary operator difficult to understand, I use it a lot (especially with Flutter) and of course your solution is better to avoid code duplication, however this operator is often misunderstood by beginners, the proof the author of the question didn't understand your answer
1

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 );
           } );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.