0

I have a PHP code:

$array['cows'] = 4;
$array['horses'] = 7;
$array['cats'] = 9;
$array['dogs'] = 2;

arsort($array);

How to do it in Java?

Sorted (from max) output of 'print_r($array);' is:

Array
(
    [cats] => 9
    [horses] => 7
    [cows] => 4
    [dogs] => 2
)
thirdknown
  • 13
  • 5

1 Answers1

0

For PHP associative array you should use Map type in Java (HashMap or TreeMap). Sorting of this types described in this article

Community
  • 1
  • 1
max
  • 2,757
  • 22
  • 19
  • Sorting maps by value is not a fun exercise. I would suggest extracting the entry set and sorting it using a comparator that looks at the entry value. – jgitter May 13 '14 at 12:17
  • I agree with you, but it's the closest analog to PHP function – max May 13 '14 at 12:36