同时排序多个阵列 [英] Sorting multiple arrays simultaneously

查看:149
本文介绍了同时排序多个阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我制定了需要处理多个阵列的程序。有没有什么办法将所有这些数组进行排序,以反映一个阵列的排序?这些值在所有三个阵列相同的索引位置,需要排序后留在相同的索引值

So i am making a program that needs to handle multiple arrays. Is there any way to sort all these arrays to reflect the sorting of one array? These values are at the same index position across all three arrays and need to stay at the same index value after sorting

例如:

我有三个数组:

String[] distance = [1,3,6,7,9];
String[] name = [Joel, John, Joe, Jill, Jane]
String[] values = [1.5,2.3,5.6,7.1,6.5];

有没有什么办法的距离数组进行排序,然后反映了排序到另一个阵列。所以,如果我按名称排序和简变为0,在其​​他阵列的相同位置的其他值也将转移到0。我怎么能这样做呢?

Is there any way to sort the distance array and then reflect that sorting to the other arrays. So if I sort by name and Jane becomes 0, the other values at the same positions in the other arrays will also move to 0. How could I do this?

推荐答案

一个更好/更面向对象的办法是有一个对象抱在一起的3场并让它排序你需要哪个领域。

A better/more object oriented approach could be to have an object holding each of the 3 fields and having it sortable on whichever field you need.

public static class MyObject implements Comparable<MyObject> {

    public int distance;
    public String name;
    public float value;


    // Replace this with whichever field is needed
    @Override
    public int compareTo(MyObject o) {
        // If it's the String
        return this.name.compareTo(o.name);
        // If it's one of the values
        return this.distance - o.distance;
    }
}

这篇关于同时排序多个阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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