0

I need to sort the results from a dns query first by priority, and then within the records that have the biggest priority values, i want to sort by the lowest weight. So for example:

Let's say I get the following data back from my dns_get_record() query:

 { 
    [0]=> array(8) 
    { 
        ["host"]=> string(27) "_sip._udp.test.123.test.net" 
        ["class"]=> string(2) "IN" 
        ["ttl"]=> int(360) 
        ["type"]=> string(3) "SRV" 
        ["pri"]=> int(10) 
        ["weight"]=> int(100) 
        ["port"]=> int(5060) 
        ["target"]=> string(22) "mysip1.123.test.net" 
    } 
    [1]=> array(8) 
    { 
        ["host"]=> string(27) "_sip._udp.test.123.test.net" 
        ["class"]=> string(2) "IN" 
        ["ttl"]=> int(360) 
        ["type"]=> string(3) "SRV" 
        ["pri"]=> int(20) 
        ["weight"]=> int(100) 
        ["port"]=> int(5060) 
        ["target"]=> string(23) "mysip2.123.test.net" 
    } 
    [2]=> array(8) 
    { 
        ["host"]=> string(27) "_sip._udp.test.123.test.net" 
        ["class"]=> string(2) "IN" 
        ["ttl"]=> int(360) 
        ["type"]=> string(3) "SRV" 
        ["pri"]=> int(20) 
        ["weight"]=> int(200) 
        ["port"]=> int(5060) 
        ["target"]=> string(23) "mysip3.123.test.net" 
    } 
} 

What I want to end up with is this:

 { 
    [0]=> array(8) 
    { 
        ["host"]=> string(27) "_sip._udp.test.123.test.net" 
        ["class"]=> string(2) "IN" 
        ["ttl"]=> int(360) 
        ["type"]=> string(3) "SRV" 
        ["pri"]=> int(20) 
        ["weight"]=> int(100) 
        ["port"]=> int(5060) 
        ["target"]=> string(23) "mysip2.123.test.net" 
    } 

    [1]=> array(8) 
    { 
        ["host"]=> string(27) "_sip._udp.test.123.test.net" 
        ["class"]=> string(2) "IN" 
        ["ttl"]=> int(360) 
        ["type"]=> string(3) "SRV" 
        ["pri"]=> int(20) 
        ["weight"]=> int(200) 
        ["port"]=> int(5060) 
        ["target"]=> string(23) "mysip3.123.test.net" 
    } 
    [2]=> array(8) 
    { 
        ["host"]=> string(27) "_sip._udp.test.123.test.net" 
        ["class"]=> string(2) "IN" 
        ["ttl"]=> int(360) 
        ["type"]=> string(3) "SRV" 
        ["pri"]=> int(10) 
        ["weight"]=> int(100) 
        ["port"]=> int(5060) 
        ["target"]=> string(22) "mysip1.123.test.net" 
    } 

} 

Notice that I now have: 1. The priorities sorted from biggest number to smallest.... 2. Within the higher priority records, the one with the smaller value in weight is listed first.

Code

I have written code to do the first sort, where items with the higher priority values are at the top. This is what my code looks like so far:

 $results=dns_get_record("test.123.test.net", DNS_NAPTR);
 $answer = $results[0]["replacement"];
 $sipservers = dns_get_record($answer,DNS_SRV);

And then to sort:

    private function comp($a, $b) {
            if ($a['pri'] == $b['pri']) {
                    return $a['weight'] - $b['weight'];
            }
            return strcmp($a['pri'], $b['weight']);
    }

usort($sipservers, 'comp');
echo("<BR><BR> after sort: <BR><BR>");
var_dump($sipservers);

But it's sorted ascending for fields. This sort is based on a post I found here: PHP usort sorting multiple fields

I'm still trying to review this to better understand what it's doing. I've clearly not adapted it properly for my use case.

Any suggestions would be appreciated. Thanks.

Community
  • 1
  • 1
dot
  • 14,928
  • 41
  • 110
  • 218
  • [array_multisort](http://php.net/manual/en/function.array-multisort.php) should do the trick. – Andrei P. Feb 11 '15 at 16:35
  • @AndreiP, can you expand with an example on how i can change my current use of that function or use it again? – dot Feb 11 '15 at 16:38
  • Ah I think I just need to reverse a- b to b- a. Will try after lunch – dot Feb 11 '15 at 17:41

1 Answers1

0

Silly mistake on my part. I thought I did, but I missed swapping around the following:

return $a['weight'] - $b['weight'];

That is now:

return $b['weight'] - $a['weight'];

Thanks.

dot
  • 14,928
  • 41
  • 110
  • 218