gpt4 book ai didi

java - 如何使用各自的数组 ID 或值对数组进行排序?

转载 作者:行者123 更新时间:2023-11-30 06:32:28 25 4
gpt4 key购买 nike

我有两个数组:

 String [] ids= new String [5];
String [] points= new String [5];

String one="a,b,c,d,e";
//or
String one="nepal,japan,finland ,brazil,spain";
String two="100,500,200,400,300";
ids= one.split(",");
points= two.split(",");

现在我想按降序对 points 进行排序。

500,400,300,200,100 with respective ids b,d,e,c,a

我该怎么做。

但我尝试按降序排列,这没问题,但是如何使用各自的 id 对 id 进行排序。

 Arrays.sort(points, Collections.reverseOrder());
Arrays.toString(points);

Arrays.sort(points);

如果我做同样的 ids 也是降序的。但这不是输出。

已编辑:

如果我有替换

a,b,c,d,e 给国家名称或时间的字母。

String one="a,b,c,d,e";
// or
String countries = "nepal,japan,finland,brazil,spain";
// or
String time="0:10,1:25,4:00,2:10,0:55";

最佳答案

你可以定义你自己的Comparator,像这样:

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

public class CustomArrayComparator implements Comparator<String>
{
private String[] pointsArray;
private Map<String, Integer> idsMap;

public CustomArrayComparator(String[] pointsArray, String[] countriesArray)
{
this.pointsArray = pointsArray;
idsMap = new HashMap<String, Integer>();
for(int i = 0; i < pointsArray.length; i++) idsMap.put(countriesArray[i], i);
}

@Override
public int compare(String s1, String s2)
{
return pointsArray[idsMap.get(s2)].compareTo(pointsArray[idsMap.get(s1)]);
}
}

这里是主要的:

String[] points = {"100", "500", "200", "400", "300"};
String[] countries = {"nepal", "japan", "finland", "brazil", "spain"};
CustomArrayComparator comparator = new CustomArrayComparator(points, countries);
Arrays.sort(countries, comparator);
Arrays.sort(points, Collections.reverseOrder());

System.out.println(Arrays.toString(points));
System.out.println(Arrays.toString(countries));

输出:

[500, 400, 300, 200, 100]
[japan, brazil, spain, finland, nepal]

编辑:

作为通用类,这里是具有通用实现的同一个类:

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

public class CustomArrayComparator<T extends Comparable<T>, E> implements Comparator<T>
{
private T[] mainArray;
private Map<E, Integer> indexesMap;

public CustomArrayComparator(T[] mainArray, E[] associatedArray)
{
this.mainArray = mainArray;
indexesMap = new HashMap<E, Integer>();
for(int i = 0; i < mainArray.length; i++)
{
indexesMap.put(associatedArray[i], i);
}
}

@Override
public int compare(T t1, T t2)
{
return mainArray[indexesMap.get(t2)].compareTo(mainArray[indexesMap.get(t1)]);
}
}

主要内容:

String[] points = {"100", "500", "200", "400", "300"};
String[] countries = {"nepal", "japan", "finland", "brazil", "spain"};
Comparator<String> comparator = new CustomArrayComparator<String, String>(points, countries);
Arrays.sort(countries, comparator);
Arrays.sort(points, Collections.reverseOrder());

System.out.println(Arrays.toString(points));
System.out.println(Arrays.toString(countries));

输出:

[500, 400, 300, 200, 100]
[japan, brazil, spain, finland, nepal]

关于java - 如何使用各自的数组 ID 或值对数组进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8775807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com