简体   繁体   English

如何模仿Java中PHP的关联数组排序?

[英]How to imitate PHP's sorting of associative array in Java?

I have a PHP code: 我有一个PHP代码:

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

arsort($array);

How to do it in Java? 用Java怎么做?

Sorted (from max) output of 'print_r($array);' (从最大)排序输出'print_r($ array);' is: 是:

Array
(
    [cats] => 9
    [horses] => 7
    [cows] => 4
    [dogs] => 2
)

For PHP associative array you should use Map type in Java (HashMap or TreeMap). 对于PHP关联数组,应在Java中使用Map类型(HashMap或TreeMap)。 Sorting of this types described in this article 本文介绍的这种类型的排序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM