object arrays
object arrays
JAVA – how to sort 1 object arraylist and keep it in sync with an array of primitives?
I want to store an arraylist of objects, (students), and sort it. At the same time I want to store a primitive array of doubles, (test average results), which will be kept in sync with the arraylist element positions. Think this is possible using a “tag sort”?? Anyone provide any decent examples of advice on how to proceed? TIA
use the Colt library, this works. Assuming you capture the student details including say 5 exam marks, you can calculate the averages for each student, then populate an array of doubles called Averages. so the zeroth element of Averages you have the mean average mark for the zeroth student ie. exam results/5. then go off to colt library and get the classes mentioned at the bottom of my post and do this…
x = StudentArray;
y = Averages;
Swapper swapper = new Swapper()
{
public void swap(int a, int b)
{
StudentTestResults t1; double t2;
t1 = x[a]; x[a] = x[b]; x[b] = t1;
t2 = y[a]; y[a] = y[b]; y[b] = t2;
}
};
IntComparator comp = new IntComparator()
{
public int compare(int a, int b)
{
return y[a] == y[b] ? 0 :
(y[a] < y[b] ? - 1 : 1);
}
};
System.out.println("before:");
for(int i = 0; i < x.length; i++)
{
System.out.println("Student" + x[i].Display());
System.out.println("Average="+ y[i]);
}
GenericSorting.quickSort(0, x.length, comp, swapper);
System.out.println("after:");
for(int i = 0; i < x.length; i++)
{
System.out.println("Student" + x[i].Display());
System.out.println("Average="+ y[i]);
}
}
you can find the class's Swapper, GenericSort and Comparator here:
http://dsd.lbl.gov/~hoschek/colt/api/cern/colt/package-summary.html
object arrays