In the past few months, I’ve taken up learning Java for self-enrichment. A friend and I got an idea for a cool text-based game, so I started coding it, but one portion of the code has me stumped.
I have two arrays; one is a String and the other is an int (let’s just call them String and int). I’d like to be able to sort the elements of int in ascending order and have the elements of String sort with int. Conversely, I’d also like to sort String alphabetically and have the elements of int stay connected to its partner in String.
Just an example, since I don’t think I made myself clear:
Any help I can get would be appreciated. Thank you in advance.
Edit: I might add that I’ve done quite a bit of research on the 'net. I’ve come across the built-in Arrays.sort( ) method, but I receive a runtime error when I try to use Arrays.toString( ). Additionally, nothing I’ve found shows how to link the elements in the two arrays. - A.F.
My advice is to not make separate arrays of ints and strings. Create a class that has a string and an int as private members, and make two accessor functions. Then create the objects that you need, put them in arrays as necessary, sorted in the order you prefer.
I don’t think you can get away without doing something like this to match up each int with its corresponding string (and vice versa). You could do it with some pointer equivalent, but the potential for bugs is so high that it doesn’t make sense. So yeah, what he said.
As mentioned above - and look in your reference book for information about a package called java.util - comes standard with java. (Not positive about my own use of terminology). You can create a custom class that implements interface Comparator which is then used with a static function from class Arrays - simple, no?
But once you understand that, you’ll know a lot more about Java.
class Player {
String _name;
int _highscore;
}
class SortPlayers implements java.util.Comparator<Player> {
// member
int _field;
// constructor
public SortPlayers(int field) {
_field = field;
}
// interface method
public int compare(Player player1, Player player2) {
if (_field == 0) return player1._name.compare(player2._name);
else return player1._highscore - player2._highscore;
}
// interface method - needed only to comply with interface requirements - little other practical use
public boolean equals(Object o) { return true; }
}
class Game {
//...
Player[] arPlayers;
//...
void sortPlayers(int field) {
Arrays.sort(arPlayers, new SortPlayers(field));
}
}
This is a reasonable solution, but rather than using an int to represent which field you want to sort by, use a typesafe enum. The cool thing about Java enums is that they actually have behavior. In this case, you can declare each enum to have a member of type Comparator<Player> and implement the proper comparator in the enum declaration as an anonymous class, thus:
import java.util.Arrays;
import java.util.Comparator;
public class Player {
private String _name;
private int _score;
public Player(String name, int score) {
super();
_name = name;
_score = score;
}
public String getName() {
return _name;
}
public void setName(String _name) {
this._name = _name;
}
public int getScore() {
return _score;
}
public void setScore(int _score) {
this._score = _score;
}
public static void sortPlayers(Player[] players, Player.SortType sortType) {
Arrays.sort(players, sortType.getComparator());
}
public enum SortType {
ByScore(new Comparator<Player>() {
public int compare(Player p1, Player p2) {
return p1.getScore() - p2.getScore();
}
}),
ByName(new Comparator<Player>() {
public int compare(Player p1, Player p2) {
return p1.getName().compareTo(p2.getName());
}
});
private Comparator<Player> _comparator;
private SortType(Comparator<Player> comparator) {
_comparator = comparator;
}
public Comparator<Player> getComparator() {
return _comparator;
}
}
}
This is somewhat more elegant, as adding new ways of sorting can be done directly in the enum declaration, and you never have to worry about what each integer code for the sort field means. No messy if-elseif or case-switch statements either.