Let's have simplified class:

```
class A
{
   bool val_;
   public:
     A() : val_(true) {}
     bool isNew() const { return val_; }
     void setDirty() { val_ = false; }
};

```

and the vector of objects of such class:

```
vector<A> coll;
coll.push_back(A());
coll.push_back(A());
coll.push_back(A());
coll.push_back(A());
coll[1].setDirty();
coll[3].setDirty();

```

I need some elegant solution to rearrange(sort) elements in the vector, so that not modified objects will be grouped at the beginning of the sequence.
['https://Stackoverflow.com/questions/7718418', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/801620/']
How about `sort`:

```
#include <algorithm>

std::sort(coll.begin(), coll.end(),
     [](const A & a, const A & b) -> bool { return a.isNew() < b.isNew(); } );

```

You'll have to rewrite the class to declare `isNew()` as `const`.

For older compilers, use a function instead of the lambda:

```
bool isNewCompare(const A & a, const A & b) { return a.isNew() < b.isNew(); }
std::sort(coll.begin(), coll.end(), isNewCompare);

```

*Edit:* @Vladimir has the better answer, `std::partition()` is the more appropriate algorithm for this problem.
`std::sort` lets you provide a custom comparison function object. You define a class that overrides the paranthesis operator, and returns true if the first argument should come before the right argument:

```
class COrderByDirty
{
   bool operator(const A& lhs, const A& rhs) const 
   {
       // Says lhs should come before rhs only if 
       // lhs is marked as dirty, and rhs is not
       if (lhs.GetDirty() < rhs.Dirty())
       {
           return true;
       }
   }
 }

```

Then simply instantiate it use it to sort:

```
 std::sort(coll.begin(), coll.end(), COrderByDirty());

```

If you can use C++11, you can avoid the lengthy class creation and use a lambda, as Kernek does in his answer.