如何使用相应的数组ID或值对数组进行排序?

时间:2012-01-08 06:19:45

标签: java sorting comparator

我有两个数组:

 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";

3 个答案:

答案 0 :(得分:2)

Arrays.sort()只会对您提供的一个数组进行排序。我将创建一个包含idpoint字段的类,该类的数组(或Collection),并对该数组进行排序。您还必须实施comparable

或者,您可以在两个数组之间建立映射。对points数组进行排序,然后重新排序ids数组以匹配映射。 (这稍贵但可能更容易理解。)

答案 1 :(得分:2)

您可以定义自己的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]

答案 2 :(得分:1)

定义一个自定义类(或类似于Pair&lt; T1,T2&gt;的通用类,并定义一个按第一个值排序的比较器。然后你可以做Arrays.sort(arrayOfPairs,pairComparator)