Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按一个数组的值对两个单独的Java数组进行排序_Java_Arrays_Sorting - Fatal编程技术网

按一个数组的值对两个单独的Java数组进行排序

按一个数组的值对两个单独的Java数组进行排序,java,arrays,sorting,Java,Arrays,Sorting,我目前正在尝试对两个单独的数组按值排序,然后按字母名称排序。我该怎么做 String[] players = new String[5]; int[]goalsByPlayer = new int[5]; players = {a, b, c, d, e}; goalsByPlayer = {3, 0, 0, 0, 0}; 1. a 3 goals 2. b 0 goals 3. c 0 goals 4. d 0 goals 5. e 0 goals 如果我把c的目标数量改为4,那么列表应该

我目前正在尝试对两个单独的数组按值排序,然后按字母名称排序。我该怎么做

String[] players = new String[5];
int[]goalsByPlayer = new int[5];
players = {a, b, c, d, e};
goalsByPlayer = {3, 0, 0, 0, 0};

1. a 3 goals
2. b 0 goals
3. c 0 goals
4. d 0 goals
5. e 0 goals
如果我把c的目标数量改为4,那么列表应该是

1. c 4 goals
2. a 3 goals
3. b 0 goals
4. d 0 goals
5. e 0 goals
请发送建议

编辑: 这是我写的代码

for (int w = 0; w < number; w++) {
                            goalsByPlayer[w] *= -1;
                        }

                        Arrays.sort(sort);

                        for (int w = 0; w < number; w++) {
                            goalsByPlayer[w] *= -1;
                        }

for (int p = 0; p < number; p++) {

                            System.out.println((p + 1) + ". " + players[p] + " " + goalsByPlayer[p] + " goals");


                        }

将它们添加到映射并使用Java8中提供的流

String[] players = { "a", "b", "c", "d", "e" };
int[] goalsByPlayer = { 3, 0, 0, 4, 0 };
Map<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < players.length; i++) map.put(players[i], goalsByPlayer[i]);
Map<String, Integer> sortedMap = new LinkedHashMap<>();
map.entrySet().stream().sorted(Map.Entry.<String, Integer>comparingByValue().reversed()).forEachOrdered(e -> sortedMap.put(e.getKey(), e.getValue()));
for (String key : sortedMap.keySet()) System.out.println("Player: " + key + " goals: " + sortedMap.get(key));

最好的方法是将两个值(“玩家”和“目标”)放入一个对象(您创建的类)中,并对其进行排序。否则,请展示您编写的代码,我们将提供帮助。建议-阅读java.util.Comparator接口:
public static void main(String[] args) {
    // Input data
    String[] players = {"a","b","c","d","e"};
    int[] goalsByPlayer = {3,0,0,4,0};

    // Put all of them into Map
    Map<String, Integer> unsortMap = new HashMap<>();
    for (int i = 0; i < players.length; i++) {
        unsortMap.put(players[i], goalsByPlayer[i]);
    }

    System.out.println("Unsort Map......");
    printMap(unsortMap);

    // Sort by value with reverse order
    Map sorted = unsortMap.entrySet().stream()
        .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
            (oldValue, newValue) -> oldValue, LinkedHashMap::new));

    System.out.println("\nSorted Map......By Value");
    printMap(sorted);
}

// print a map
public static <K, V> void printMap(Map<K, V> map) {
    for (Map.Entry<K, V> entry : map.entrySet()) {
        System.out.println("Key : " + entry.getKey()
            + " Value : " + entry.getValue());
    }
}
Unsort Map......
Key : a Value : 3
Key : b Value : 0
Key : c Value : 0
Key : d Value : 4
Key : e Value : 0

Sorted Map......By Value
Key : d Value : 4
Key : a Value : 3
Key : b Value : 0
Key : c Value : 0
Key : e Value : 0
String[] players = { "a", "b", "c", "d", "e" };
int[] goalsByPlayer = { 3, 0, 0, 4, 0 };
Map<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < players.length; i++) map.put(players[i], goalsByPlayer[i]);
Map<String, Integer> sortedMap = new LinkedHashMap<>();
map.entrySet().stream().sorted(Map.Entry.<String, Integer>comparingByValue().reversed()).forEachOrdered(e -> sortedMap.put(e.getKey(), e.getValue()));
for (String key : sortedMap.keySet()) System.out.println("Player: " + key + " goals: " + sortedMap.get(key));
Player: d goals: 4
Player: a goals: 3
Player: b goals: 0
Player: c goals: 0
Player: e goals: 0