While using two separate arrays and keeping their sort in sync is possible, using this type of solution may lead to bugs that are hard to find later on. For example, if the syncs between the arrays don't work correctly, then the wrong weights may be matched with the heights.
One way to avoid this type of problem is to encapsulate the height/weight in a class so they will always be in sync. In Figure 1 there is a class named Person
that has height, weight, and name as attributes. If you are always going to sort by height ascending, then you can implement the compareTo()
method as shown in Figure 1.
Figure 2 shows a junit test case to demonstrate how to sort a list of Person
s. The test case also demonstrates how to sort by weight. In both cases there is never a sync issue between weight and height since the sort is on the object that encapsulates them.
Figure 1 - Person
class
public class Person implements Comparable {
private Float height;
private Float weight;
private String name;
public Person(){}
public Person(Float height, Float weight, String name) {
this.height = height;
this.weight = weight;
this.name = name;
}
public Float getHeight() {
return height;
}
public void setHeight(Float height) {
this.height = height;
}
public Float getWeight() {
return weight;
}
public void setWeight(Float weight) {
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int compareTo(Person other) {
//sort by height ascending
return this.height.compareTo(other.getHeight());
}
}
Figure 2 - junit test class
import junit.framework.TestCase;
import java.util.*;
public class PersonTest extends TestCase {
private List personList = new ArrayList();
public PersonTest(String name) {
super(name);
}
public void testCompareTo() {
personList.add(new Person(72F,125F,"Bob"));// expect 3rd when sorted by height asc
personList.add(new Person(69.9F,195F,"Jack"));// expect 2nd when sorted by height asc
personList.add(new Person(80.05F,225.2F,"Joe"));// expect 4th when sorted by height asc
personList.add(new Person(57.02F,89.9F,"Sally"));// expect 1st when sorted by height asc
Collections.sort(personList);
assertEquals("Sally should be first (sorted by height asc)",personList.get(0).getName(),"Sally");
assertEquals("Jack should be second (sorted by height asc)",personList.get(1).getName(),"Jack");
assertEquals("Bob should be third (sorted by height asc)",personList.get(2).getName(),"Bob");
assertEquals("Joe should be fourth (sorted by height asc)",personList.get(3).getName(),"Joe");
Collections.sort(personList,new Comparator() {
public int compare(Person p1, Person p2) {
//sort by weight ascending
return p1.getWeight().compareTo(p2.getWeight());
}
});
assertEquals("Sally should be first (sorted by weight asc)",personList.get(0).getName(),"Sally");
assertEquals("Bob should be second (sorted by weight asc)",personList.get(1).getName(),"Bob");
assertEquals("Jack should be third (sorted by weight asc)",personList.get(2).getName(),"Jack");
assertEquals("Joe should be fourth (sorted by weight asc)",personList.get(3).getName(),"Joe");
}
}