4

I currently have 3 arrays of information and am unsure of how to sort them based on one of the values:

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.

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?

3
  • 5
    Yes. Yes, an extra class is necessary. (But people consistently overestimate how much an extra class actually "costs" in terms of code, effort, etc.) Commented Nov 19, 2012 at 19:37
  • 1
    The question is, is 3 arrays really necessary. Commented Nov 19, 2012 at 19:39
  • Won't you like to work with objects or with indexes? Commented Nov 19, 2012 at 19:46

3 Answers 3

10

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. 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.

Sign up to request clarification or add additional context in comments.

1 Comment

It seems like everyone is on the seem page here. Thanks for clarifying and making the point of keeping the code maintainable
3

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.

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);

1 Comment

Thank you for the fine example, it looks like a nice simple clean way to write the class and I'll probably do it just like that.
2

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? 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.

1 Comment

Your first paragraph was really helpful in understanding why the best option is to write an extra class. It is definitely beneficial in the long run writing the extra class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.