0

So I need to implement sorting show on this website:

http://news.bbc.co.uk/sport2/hi/olympics/medals_table/default.stm

And part of my array looks like this:

[1] => Array
    (
        [country_id] => 3
        [country_name] => Russia
        [gold] => 23
        [silver] => 21
        [bronze] => 28
        [total] => 72
    )

[2] => Array
    (
        [country_id] => 189
        [country_name] => USA
        [gold] => 36
        [silver] => 38
        [bronze] => 36
        [total] => 10
    )

[3] => Array
    (
        [country_id] => 230
        [country_name] => Germany
        [gold] => 16
        [silver] => 10
        [bronze] => 15
        [total] => 41
    )

[4] => Array
    (
        [country_id] => 231
        [country_name] => China
        [gold] => 51
        [silver] => 21
        [bronze] => 28
        [total] => 100
    )   

So I need to sort array by gold,silver,bronze and then country_name.

Does anyone have some idea for this ?

3
  • php.net/manual/en/function.usort.php + custom callback Commented Mar 19, 2012 at 18:32
  • What are your sorting parameters? It looks like individual weights have been given to each metal type, like a point system. Then you sort based on total points. Commented Mar 19, 2012 at 18:37
  • this is for olimpic games please see the link in question body. Commented Mar 19, 2012 at 18:38

3 Answers 3

1

use uasort function :

$arr = array(
  Array(
    'country_id' => 3,
    'country_name' => 'Russia',
    'gold' => 23,
    'silver' => 21,
    'bronze' => 28,
    'total' => 72,
  ),
  Array(
    'country_id' => 189,
    'country_name' => 'USA',
    'gold' => 36,
    'silver' => 38,
    'bronze' => 36,
    'total' => 10,
  ),
  Array(
    'country_id' => 230,
    'country_name' => 'Germany',
    'gold' => 16,
    'silver' => 10,
    'bronze' => 15,
    'total' => 41,
  ),
  Array(
    'country_id' => 231,
    'country_name' => 'China',
    'gold' => 51,
    'silver' => 21,
    'bronze' => 28,
    'total' => 100,
  )
);

function mySort($a,$b) {
    if ($b['gold'] == $a['gold']) {
        if ($b['silver'] == $a['silver']) {
            if ($b['bronze'] == $a['bronze']) {
                return strcmp($b['country_name'], $a['country_name']);
            } else {
                return $b['bronze'] - $a['bronze'];
            }
        } else {
            return $b['silver'] - $a['silver'];
        }
    } else {
        return $b['gold'] - $a['gold'];
    }
}

uasort($arr, 'mySort');
print_r($arr);

output:

Array
(
[1] => Array
    (
        [country_id] => 189
        [country_name] => USA
        [gold] => 36
        [silver] => 38
        [bronze] => 36
        [total] => 10
    )

[0] => Array
    (
        [country_id] => 3
        [country_name] => Russia
        [gold] => 23
        [silver] => 21
        [bronze] => 28
        [total] => 72
    )

[3] => Array
    (
        [country_id] => 231
        [country_name] => China
        [gold] => 51
        [silver] => 21
        [bronze] => 28
        [total] => 100
    )

[2] => Array
    (
        [country_id] => 230
        [country_name] => Germany
        [gold] => 16
        [silver] => 10
        [bronze] => 15
        [total] => 41
    )

)
Sign up to request clarification or add additional context in comments.

2 Comments

I will analyze this - thanks but your output is not ok :) Since chine has most gold medals China should be on first place - news.bbc.co.uk/sport2/hi/olympics/medals_table/default.stm
@Splendid: yes, corrected. I first sort ascending, then invert all $a and $b except one :(
0

I know of two options:

  1. usort() to sort the array by values using a user-defined comparison function;
  2. ksort() to sort the array by key.

Hope this answer is helpful enough.

Comments

0

Probably there is shorter way u can find. But this may also help

function my_sort($a, $b) {
    if($a['gold'] > $b['gold']) {
        return -1;
    }
    elseif ($a['gold'] == $b['gold']) {
        if($a['silver'] > $b['silver']) {
            return -1;
        }
        elseif ($a['silver'] == $b['silver']) {
          if ($a['bronze'] > $b['bronze']) {
              return -1;
            }
            elseif ($a['bronze'] == $b['bronze']) {
            return strcmp($b['country_name'], $a['country_name']);
           }
          return 1;
        }
      return 1;
    }
    return 1;
}

usort($arr, 'my_sort');

print_r($arr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.