使用 Arrays.sort() 对并行数组进行排序 [英] Sort a parallel array using Arrays.sort()

查看:29
本文介绍了使用 Arrays.sort() 对并行数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 Arrays.sort() 对数组进行排序,然后将另一个相关数组与已排序数组的位置相同,例如:

Is it possible to sort an array using Arrays.sort() and thereafter have another related array positioned the same as the sorted array for example:

    String arrNames[] = new String[5];
    String arrCellNo[] = new String[arrNames.length];


    String arrNamesSorted[] = new String[arrNames.length];
    System.arraycopy(arrNames, 0, arrNamesSorted, 0, arrNames.length);
    Arrays.sort(arrNamesSorted);

从这一点来看,我想要做的是对 CellNo 数组进行排序,这样如果person"有一个 cellNox",那么他在数组 arrNames 已排序

From this point what i would like to do is sort the CellNo array such that if "person" had a cellNo "x", he will have the same "cellNo" "x" after the array arrNames is sorted

推荐答案

我会采用不同的方法:

  1. 创建一个新对象:

  1. Create a new object:

public class Person {
private name;
private cellNo;

// Implement getters and setters
}

  • 创建一个比较器:

  • Create a comparator:

    public MyComparator implements Comparator<Person> {
         public int compare(Person a, Person b) { 
    
               return a.getName().compareTo(b.getName());
         }
    }
    

  • Person[] people = ... 数组上调用 Array.sort(persons, new MyComparator())

    这篇关于使用 Arrays.sort() 对并行数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆