I have an ArrayList of Objects that I'd like to sort by their value. Basically I have 9 different mathematical functions (i.e., f1(n) = log n, f2(n) = n, f3(n) = n log n, etc). I plugged the value of 1 into all 9 functions and I placed their results in an ArrayList of Objects with their label attached to them as shown in the code below. I'd like to sort the entire list of results.
ArrayList<Object>val1 = new ArrayList<Object>();
val1.add("f\u2081(1) = " + log(funcValues[0]));
val1.add("f\u2082(1) = " + funcValues[0]);
val1.add("f\u2083(1) = " + exponent(funcValues[0]));
val1.add("f\u2084(1) = " + f4(funcValues[0]));
val1.add("f\u2085(1) = " + squared(funcValues[0]));
val1.add("f\u2086(1) = " + cubed(funcValues[0]));
val1.add("f\u2087(1) = " + twoN(funcValues[0]));
val1.add("f\u2088(1) = " + factorial(funcValues[0]));
val1.add("f\u2089(1) = " + f9(funcValues[0]));
Basically, wherever you see log, funcValues, exponent, f4, squared, etc those are all functions that compute the answers to the mathematical functions. The output of this ArrayList is:
f₁(1) = 0.0
f₂(1) = 1
f₃(1) = 1.0
f₄(1) = 0.0
f₅(1) = 1
f₆(1) = 1.0
f₇(1) = 2.0
f₈(1) = 1
f₉(1) = 0.0
I'd like to sort only the numbers. I was trying to do it this way:
class ValuesSorted implements Comparator<Object> {
@Override
public int compare(Object v1, Object v2) {
if ()
return 0;
}
}
I am stuck on the if statement because I can't do something like if (v1.getValue > v2.getValue) because I am using 9 different function calls to pull each of those values.
equals,hashCode, andtoStringmethods and implement aComparatorfor that class that sorts based on the value field.