<p>Thanks for a <a href="https://stackoverflow.com/questions/327893/how-to-write-a-compare-function-for-qsort-from-stdlib">solution in C</a>,
now I would like to achieve this in C++ using std::sort and vector:</p>

<pre><code>typedef struct
{
  double x;
  double y;
  double alfa;
} pkt;
</code></pre>

<p><code>vector&lt; pkt &gt; wektor;</code> filled up using push_back(); compare function:</p>

<pre><code>int porownaj(const void *p_a, const void *p_b)
{
  pkt *pkt_a = (pkt *) p_a;
  pkt *pkt_b = (pkt *) p_b;

  if (pkt_a-&gt;alfa &gt; pkt_b-&gt;alfa) return 1;
  if (pkt_a-&gt;alfa &lt; pkt_b-&gt;alfa) return -1;

  if (pkt_a-&gt;x &gt; pkt_b-&gt;x) return 1;
  if (pkt_a-&gt;x &lt; pkt_b-&gt;x) return -1;

  return 0;
}

sort(wektor.begin(), wektor.end(), porownaj); // this makes loads of errors on compile time
</code></pre>

<p>What is to correct? How to use properly std::sort in that case?</p>

```
std::sort
```
takes a different compare function from that used in
```
qsort
```
. Instead of returning –1, 0 or 1, this function is expected to return a
```
bool
```
value indicating whether the first element is less than the second.
You have two possibilites: implement
```
operator <
```
for your objects; in that case, the default
```
sort
```
invocation without a third argument will work; or you can rewrite your above function to accomplish the same thing.
Notice that you have to use strong typing in the arguments.
Additionally, it's good not to use a function here at all. Instead, use a function object. These benefit from inlining.
```
```
struct pkt_less {
    bool operator ()(pkt const& a, pkt const& b) const {
        if (a.alfa < b.alfa) return true;
        if (a.alfa > b.alfa) return false;

        if (a.x < b.x) return true;
        if (a.x > b.x) return false;

        return false;
    }
};

// Usage:

sort(wektor.begin(), wektor.end(), pkt_less());
```
```