Java 同时对两个ArrayList进行排序

Java 同时对两个ArrayList进行排序,java,arrays,sorting,arraylist,comparator,Java,Arrays,Sorting,Arraylist,Comparator,假设我有两个ArrayList: name: [Four, Three, One, Two] num: [4, 3, 1, 2] 如果我选择:Arrays.sort(num),那么我有: name: [Four, Three, One, Two] num: [1, 2, 3, 4] 是否有任何方法可以对num进行排序,并将其反映在name中,以便最终得到: name: [One, Two, Three, Four] num: [1, 2, 3, 4] ??请帮帮我。我想到了比较器和对

假设我有两个ArrayList:

name: [Four, Three, One, Two]
num:  [4, 3, 1, 2]
如果我选择:Arrays.sort(num),那么我有:

name: [Four, Three, One, Two]
num:  [1, 2, 3, 4]
是否有任何方法可以对num进行排序,并将其反映在name中,以便最终得到:

name: [One, Two, Three, Four]
num:  [1, 2, 3, 4]

??请帮帮我。我想到了比较器和对象,但对它们几乎一无所知。

与其对实际数组进行排序,不如使用一个只包含索引的数组

a[i] = i for i = 0..n
您可以使用自定义比较器根据numeruc数组对该数组进行排序。e、 g

bool compare( int a, int b ) { return num[a] < num[b]; }
bool比较(inta,intb){returnnum[a]

因此,您可以使用这些索引对两个数组进行排序。

不必对实际数组进行排序,您可以使用仅包含索引的数组

a[i] = i for i = 0..n
您可以使用自定义比较器根据numeruc数组对该数组进行排序。e、 g

bool compare( int a, int b ) { return num[a] < num[b]; }
bool比较(inta,intb){returnnum[a]

因此,您可以使用这些索引对这两个数组进行排序。

您应该以某种方式
name
num
字段关联到一个类中,然后拥有该特定类的实例列表。在此类中,提供一个检查数值的
compareTo()
方法。如果对实例进行排序,那么名称字段也将按照您希望的顺序排列

class Entity implements Comparable<Entity> {
    String name;
    int num;
    Entity(String name, int num) {
        this.name = name;
        this.num = num;
    }
    @Override
    public int compareTo(Entity o) {
        if (this.num > o.num)
            return 1;
        else if (this.num < o.num)
            return -1;
        return 0;
    }
}
类实体实现了可比较的{
字符串名;
int-num;
实体(字符串名称,int num){
this.name=名称;
this.num=num;
}
@凌驾
公共内部比较(实体o){
如果(this.num>o.num)
返回1;
else if(this.num
测试代码可以如下所示:

public static void main(String[] args) {
    List<Entity> entities = new ArrayList<Entity>();
    entities.add(new Entity("One", 1));
    entities.add(new Entity("Two", 2));
    entities.add(new Entity("Three", 3));
    entities.add(new Entity("Four", 4));
    Collections.sort(entities);

    for (Entity entity : entities)
        System.out.print(entity.num + " => " + entity.name + " ");
}
publicstaticvoidmain(字符串[]args){
列表实体=新的ArrayList();
实体。增加(新实体(“一”,1));
增加(新实体(“两个”,2));
增加(新实体(“三”,3));
增加(新实体(“四”,4));
集合。排序(实体);
for(实体:实体)
系统输出打印(entity.num+“=>”+entity.name+”);
}
输出:

1=>1 2=>2 3=>3 4=>4


您应该以某种方式
num
字段关联到一个类中,然后拥有该特定类的实例列表。在此类中,提供一个检查数值的
compareTo()
方法。如果对实例进行排序,那么名称字段也将按照您希望的顺序排列

class Entity implements Comparable<Entity> {
    String name;
    int num;
    Entity(String name, int num) {
        this.name = name;
        this.num = num;
    }
    @Override
    public int compareTo(Entity o) {
        if (this.num > o.num)
            return 1;
        else if (this.num < o.num)
            return -1;
        return 0;
    }
}
类实体实现了可比较的{
字符串名;
int-num;
实体(字符串名称,int num){
this.name=名称;
this.num=num;
}
@凌驾
公共内部比较(实体o){
如果(this.num>o.num)
返回1;
else if(this.num
测试代码可以如下所示:

public static void main(String[] args) {
    List<Entity> entities = new ArrayList<Entity>();
    entities.add(new Entity("One", 1));
    entities.add(new Entity("Two", 2));
    entities.add(new Entity("Three", 3));
    entities.add(new Entity("Four", 4));
    Collections.sort(entities);

    for (Entity entity : entities)
        System.out.print(entity.num + " => " + entity.name + " ");
}
publicstaticvoidmain(字符串[]args){
列表实体=新的ArrayList();
实体。增加(新实体(“一”,1));
增加(新实体(“两个”,2));
增加(新实体(“三”,3));
增加(新实体(“四”,4));
集合。排序(实体);
for(实体:实体)
系统输出打印(entity.num+“=>”+entity.name+”);
}
输出:

1=>1 2=>2 3=>3 4=>4


如果没有重复的元素,则可以使用排序后的贴图,例如:

int[]num={4,3,1,2};
字符串[]名称={“四”,“三”,“一”,“二”};
TreeMap sortedMap=newtreemap();

对于(int i=0;i,如果没有重复的元素,则可以使用排序映射,如:

int[]num={4,3,1,2};
字符串[]名称={“四”,“三”,“一”,“二”};
TreeMap sortedMap=newtreemap();

对于(int i=0;i,在某些情况下,仅仅创建一个新类来根据给定列表进行多个排序没有多大意义。我创建了一个函数来实现这一点,但我已将代码发布在一个列表中,因此我不会重复它。下面是一个如何使用它的示例


用法 以下是如何使用函数对任意类型的多个列表进行排序的示例:

// The key can be any type that implements Comparable, Dupes are allowed
List<Integer> key = Arrays.asList(4, 3, 1, 2, 1);

// List Types do not need to be the same
List<String> list1 = Arrays.asList("Four", "Three", "One", "Two", "One");
List<Character> list2 = Arrays.asList('d', 'c', 'a', 'b', 'a');

// Sorts key, list1, list2 using key as the sorting key.
keySort(key, key, list1, list2);

在某些情况下,仅仅创建一个新类来根据给定列表进行多个排序没有多大意义。我已经创建了一个函数来实现这一点,但我已经将代码发布在一个列表中,因此我不会重复它。下面是一个如何使用它的示例


用法 以下是如何使用函数对任意类型的多个列表进行排序的示例:

// The key can be any type that implements Comparable, Dupes are allowed
List<Integer> key = Arrays.asList(4, 3, 1, 2, 1);

// List Types do not need to be the same
List<String> list1 = Arrays.asList("Four", "Three", "One", "Two", "One");
List<Character> list2 = Arrays.asList('d', 'c', 'a', 'b', 'a');

// Sorts key, list1, list2 using key as the sorting key.
keySort(key, key, list1, list2);

实际上我有这个确切的东西。但问题是它非常慢。要获得name的对应值,我必须做:name.get(numIndex.indexOf(num.get(value)))wont name.get(a[I])如果“a”是一个普通数组,为了得到i+1的最小元素,我不知道数组列表是否提供了一个常量查找,否则任何涉及排序的操作都会非常慢。@gran\u profaci我知道这是一个老问题,但我已经发布了一个针对任意数目的任意类型的
列表
s的实现有这个确切的东西。但问题是它相当慢。要获得name的对应值,我必须做:name.get(numIndex.indexOf(num.get(value)))wont name.get(a[I]),如果“a”是一个普通数组,为了获得i+1最小元素,我不知道数组列表是否提供了一个常量查找,否则任何涉及排序的操作都会非常慢。@gran\u profaci我知道这是一个老问题,但我已经发布了一个针对任意数目的任意键入的
列表
的实现。