<p>I have a structure:</p>

<pre><code>struct pkt_
{
  double x;
  double y;
  double alfa;
  double r_kw;
};

typedef struct pkt_ pkt;
</code></pre>

<p>A table of these structures:</p>

<pre><code>pkt *tab_pkt;

tab_pkt = malloc(ilosc_pkt * sizeof(pkt));
</code></pre>

<p>What I want to do is to sort <code>tab_pkt</code> by <code>tab_pkt.alfa</code> and <code>tab_pkt.r</code>:</p>

<pre><code>qsort(tab_pkt, ilosc_pkt, sizeof(pkt), porownaj);
</code></pre>

<p>Where porownaj is a compare function, but how to write it? Here is my "sketch" of it:</p>

<pre><code>int porownaj(const void *pkt_a, const void *pkt_b)
{
  if (pkt_a.alfa &gt; pkt_b.alfa &amp;&amp; pkt_a.r_kw &gt; pkt_b.r_kw) return 1;
  if (pkt_a.alfa == pkt_b.alfa &amp;&amp; pkt_a.r_kw == pkt_b.r_kw) return 0;
  if (pkt_a.alfa &lt; pkt_b.alfa &amp;&amp; pkt_a.r_kw &lt; pkt_b.r_kw) return -1;
}
</code></pre>

There are two parts to the problem - how to write the code, and how to compare the packet types.  You must ensure you always return a value.  Your code should also always be such that:
```
```
porownaj(&pkt_a, &pkt_b) == -porownaj(&pkt_b, &pkt_a)
```
```
Your outline comparison does not handle cases such as:
```
```
pkt_a->alfa >  pkt_b->alfa && pkt_a->r_kw <= pkt_b->r_kw
pkt_a->alfa <  pkt_b->alfa && pkt_a->r_kw >= pkt_b->r_kw
pkt_a->alfa == pkt_b->alfa && pkt_a->r_kw != pkt_b->r_kw
```
```
There is one more problem - is it appropriate to compare floating point values for exact equality?  That will depend on your application.
Mechanically, you have to convert the const void pointers to const structure pointers.  I use the explicit cast - C++ requires it, and I try to make my code acceptable to a C++ compiler even when it is really C code.
```
```
int porownaj(const void *vp1, const void *vp2)
{
     const pkt *pkt_a = (const pkt *)vp1;
     const pkt *pkt_b = (const pkt *)vp2;

     if (pkt_a->alfa >  pkt_b->alfa && pkt_a->r_kw >  pkt_b->r_kw) return 1;
     if (pkt_a->alfa == pkt_b->alfa && pkt_a->r_kw == pkt_b->r_kw) return 0;
     if (pkt_a->alfa <  pkt_b->alfa && pkt_a->r_kw <  pkt_b->r_kw) return -1;
     return 0;
 }
```
```
This does not deal with the bits that I cannot resolve since I am not party to the necessary information.  Note that, in general, multi-dimensional objects (such as complex numbers, or (x,y) or (x,y,z) coordinates) cannot simply be compared for greater than or less than or equal to.