简体   繁体   English

如何排序3个并行数组?

[英]How to sort 3 parallel arrays?

I currently have 3 arrays of information and am unsure of how to sort them based on one of the values: 我目前有3个信息数组,不知道如何根据其中一个值对它们进行排序:

int[] rank = { 1, 3, 4, 2, 5 };
String[] game = { "Snake", "Mines", "Fragged", "Siege", "Tower" };
int[] year = { 1980, 1983, 1981, 1995, 1992 };

I'm wanting to sort it by rank, and I've seen many examples of people using comparators to sort 2 parallel arrays, but I haven't seen any example for sorting more than 2. 我想按排名对它进行排序,我已经看到很多人使用比较器对2个并行数组进行排序的例子,但我没有看到任何排序超过2的例子。

My first thought was to create a class with a variable for each and then sort that object, but is an extra class really necessary for a sort? 我的第一个想法是为每个创建一个带有变量的类,然后对该对象进行排序,但是对于排序来说真的是一个额外的类吗?

My first thought was to create a class with a variable for each and then sort that object, but is an extra class really necessary for a sort? 我的第一个想法是为每个创建一个带有变量的类,然后对该对象进行排序,但是对于排序来说真的是一个额外的类吗?

It's not strictly necessary - you could definitely write some code to avoid it if you really wanted to. 这不是绝对必要的 - 如果你真的想要,你绝对可以编写一些代码来避免它。 However, I'd say it's a thoroughly good thing . 但是,我认为这是一件非常好的事情

You don't really have three collections of separate items here: you have one collection of items, each of which has three properties. 这里没有三个单独的项目集合:您有一个项目集合,每个项目都有三个属性。 So make your code match that. 所以让你的代码匹配。 Whenever you find you have parallel collections, such that a[0] is related to b[0] is related to c[0] etc, you should think about encapsulating that information in a separate class. 每当你发现你有并行集合,这样a[0]b[0]相关时就与c[0]等有关,你应该考虑将这些信息封装在一个单独的类中。 It will make your code much easier to maintain, and enforces more consistency. 它将使您的代码易于维护,并实现更高的一致性。

For example, it would make no sense for those arrays to have different lengths: but there's nothing inherent in the declaration to stop that. 例如,那些数组具有不同的长度是没有意义的:但是声明中没有任何内在的东西可以阻止它。 If you have one collection, you can't possibly have different numbers of items for the different properties, precisely because you've got one collection. 如果您有一个集合,则不可能为不同的属性提供不同数量的项目,正是因为您有一个集合。

I think creating a new class would be the cleanest solution. 我认为创建一个新类将是最干净的解决方案。 You could manually implement a new sort function to duplicate swaps to the other 2 arrays whenever you apply a swap to the first array (rank), but that gets messy very quickly. 您可以手动实现一个新的排序函数,以便在将交换应用于第一个数组(排名)时将交换复制到其他2个数组,但这会很快变得混乱。

Something like the following would be all you need: 您需要的就是以下内容:

public class Game implements Comparable<Game>{
    private int rank = 0;
    private int year = 0;
    private String name = "";
    ...
    // Constructor +
    // Usual getters and setters here
    ..
    public int compareTo(Game anotherGame) {
       return this.rank - anotherGame.getRank();
    }
}

And then you can simply do: 然后你可以简单地做:

List<Game> games = new ArrayList<Game>();
...
// Add some games to your games list
...
Collections.sort(games);

Is the extra class necessary? 额外课程是否必要? Well no, of course not. 好吧不,当然不是。 You could come up with a sorting routine that would keep everything consistent. 你可以想出一个可以保持一致性的排序程序。 However, what happens if next week you decide you need a 4th array, such as a publisher? 但是,如果您下周决定需要第4个阵列(例如发布者),会发生什么? Now your sorting routine won't work and you have to write a new one. 现在你的排序程序不起作用,你必须写一个新的。

If you instead write a class to hold these fields as properties, you can simplify the sorting logic immensely, plus you only have to worry about one array. 如果你改为编写一个类来保存这些字段作为属性,你可以极大地简化排序逻辑,而且你只需要担心一个数组。 Any extra work you do now will be recouped very quickly then next time you have to maintain this code. 您现在所做的任何额外工作都将很快得到补偿,然后您必须维护此代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM