java:基于array2排序array1

时间:2011-05-05 13:42:41

标签: java arrays sorting time-complexity comparable

感谢Zirak的帮助。在previous post我在JavaScript中实现了以下内容:

var arr1 =[0,1,2,3];
var arr2 =["ac", "bc", "ad", "e"];
var result = arr1 .sort(function(i, j){return arr2[i].localeCompare(arr2[j])})
document.write(result );

实现这一点的方法在JavaScript中非常紧凑,这样的简单实现也可以实现这一点的java实现吗?我只能想到实现Comparable接口,如下所示:

public class testCompare {
    public static String[] arr2={"ac", "bc", "ad", "e"};
    public static Obj[] arr1={new Obj(0), new Obj(1), new Obj(2), new Obj(3)};
    static class Obj implements Comparable{
            int index=0;
            public Obj(int i){
                    index=i;
            }
            @Override
            public int compareTo(Object o) {
                    return arr2[index].compareTo(arr2[((Obj)o).index]);
            }
     }
}

但是如果数组中有X个项目,那么我将不得不创建X许多Objs,还有另一种方法可以更简单地实现吗?另一个问题是,如果我执行上述方法,那么在java和JavaScript中排序的时间复杂度是O(n^2)吗?非常感谢

4 个答案:

答案 0 :(得分:4)

public class MyComparator implements Comparator<Integer> {
    @Override
    public int compare(Integer i1, Integer i2) {
        return arr2[i1.intValue()].compareTo(arr2[i2.intValue()]);
    }
}

Arrays.sort(arr1, new MyComparator());

这相当于JavaScript排序。 Comparator对象用作JavaScript中使用的回调函数。

答案 1 :(得分:3)

尝试使用TreeMap<String, Integer>(假设您要对整数进行排序),这意味着所有条目都按字符串键排序:

SortedMap<String, Integer> map = new TreeMap<String, Integer>();
map.put("ac", 0);
map.put("bc", 1);
map.put("ad", 2);
map.put("e", 3);

for( Map.Entry<String, Integer> entry : map.entrySet() )
{
  System.out.println(entry.getKey() + " - " + entry.getValue());
}

输出:

ac - 0
ad - 2
bc - 1
e - 3

要对数组进行排序并获取先前索引的新顺序,您可以迭代数组并将索引作为Integer对象添加到地图中:

String[] input = {"ab", "bc", "ad" , "e" };
SortedMap<String, Integer> map = new TreeMap<String, Integer>();
for( int i = 0; i < input.length; ++i )
{
  map.put(input[i], i); //or use values from another array, e.g. map.put(inputKeys[i], inputValues[i]);
}

如果您需要按照自然顺序以外的任何其他方式对键进行排序,则可以向Comparator<String>构造函数添加TreeMap

答案 2 :(得分:1)

public class SortA1byA2array 
{
public static void main (String[] args) 
{
int[] arr1={2,1,2,5,7,1,9,8,3,6,8,8};       
int[] arr2={2,1,8,3};   
TreeMap hm=new TreeMap();   
int count=1;
        for(int i=0;i<arr1.length;i++){
            if(hm.containsKey(arr1[i])){
                hm.put(arr1[i], ++count);
            }
            else{
                count=1;
                hm.put(arr1[i],count);
            }
        }


        for(int i=0;i<arr2.length;i++){
            if(hm.containsKey(arr2[i])){
                for(int j=0;j<(Integer)hm.get(arr2[i]);j++){
                    System.out.println(arr2[i]);
                }
                hm.remove(arr2[i]);
            }
        }

         Iterator it = hm.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry pairs = (Map.Entry)it.next();
                System.out.println(pairs.getKey());
                it.remove(); 
            }
    }
}

答案 3 :(得分:0)

回答第二部分问题:Java中的Arrays.sort保证 O(n log n)时间复杂度,如指定的in the API