-2

Please help me to sort such type of collection in PHP, below is a collection of objects which has to be sort in desecnding order.

$og = stdClass Object
(
    [datanumen[DOT]com] => 10
    [diskinternals[DOT]com] => 1
    [download[DOT]cnet[DOT]com] => 2
    [easeus[DOT]com] => 5
    [en[DOT]pstrecovery[DOT]net] => 6
    [en[DOT]pstrecovery[DOT]org] => 3
    [howto-outlook[DOT]com] => 4
    [kerneldatarecovery[DOT]com] => 2
    [mail2web[DOT]com] => 2
    [msoutlook[DOT]info] => 4
    [nirsoft[DOT]net] => 1
    [office[DOT]microsoft[DOT]com] => 14
    [officerecovery[DOT]com] => 1
    [outlook[DOT]recoverytoolbox[DOT]com] => 1
    [outlookpstrecovery[DOT]com] => 16
    [outlookrecovery-tool[DOT]com] => 1
    [outlookrecovery[DOT]net] => 1
    [pst-repair-tool-free[DOT]software[DOT]informer[DOT]com] => 1
    [pstrepair[DOT]net] => 1
    [pstrepairutility[DOT]net] => 9
    [pstscanner[DOT]com] => 11
    [recovermyemail[DOT]com] => 2
    [slipstick[DOT]com] => 4
    [social[DOT]technet[DOT]microsoft[DOT]com] => 2
    [stellarinfo[DOT]com] => 14
    [support[DOT]microsoft[DOT]com] => 15
    [techrepublic[DOT]com] => 17
)

Please help me to sort such type of collection in php

Charles
  • 1,121
  • 19
  • 30
wasim beg
  • 73
  • 13

1 Answers1

2

This is easy if you temporarily convert to an array:

$og = (array) $og;
asort( $og );
$og = (object) $og;

As an example:

$og = new stdClass;
$og->a = 5;
$og->b = 2;
$og->c = 8;
$og->d = 1;

$og = (array) $og;
asort( $og );
$og = (object) $og;

echo '<pre>';
print_r( $og );
echo '</pre>';
Brian Gottier
  • 4,522
  • 3
  • 21
  • 37