Sorting multiple arrays simultaneously

前端 未结 2 1098
轮回少年
轮回少年 2020-12-19 15:07

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 in

2条回答
  •  忘掉有多难
    2020-12-19 15:38

    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 {
    
        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;
        }
    }
    

提交回复
热议问题