4

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.

2
  • 3
    I'd suggest creating your own custom class that contains both the function and value. Override the equals, hashCode, and toString methods and implement a Comparator for that class that sorts based on the value field. Commented Sep 16, 2014 at 4:52
  • Marc- could also use a Map, see my answer. Commented Sep 16, 2014 at 5:56

3 Answers 3

2
Simply try 
Collections.sort(testList);
Collections.reverse(testList);

http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html

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

4 Comments

This is more of a comment than an answer.
@voidHead comment requires 50 reputation
@Gaurav This would not work because you cannot use Collections.sort on an Arraylist of objects.
@Gaurav This is not an exam. See the answer as something that is all-embracing on the question posed. That way you'll be able to write a good answer.
1

You are trying to track two things, the value and a label. When you are tracking two associated things, use a Map. In this case, a sorted map, i.e. a TreeMap.

TreeMap map = new TreeMap<Double, String>();
// for each function, map.put(value, label), e.g.
map.put(log(funcValues[0]), "f\u2081(1)");
...
map.put(f4(funcValues[0]),"f\u2084(1)");
...
map.put(f9(funcValues[0]), "f\u2089(1)");

And result will be sorted by numerical value. map.values() will have the labels in sorted order.

2 Comments

it seems as though when I do this, only three results output and the value does not output. For example, here is the output: [f₉(1) = , f₈(1) = , f₇(1) = ]
Hmm, that's because in this example you only have 3 distinct values, 0.0, 1.0, and 2.0. In a "real" example would there be more variety? I.e. don't use 1 as the argument to each function, as 1 squared = 1 cubed = twoToThe(1). If you really have duplicate values this answer doesn't work.
0
class Pair<X,Y>{

    private X first;
    private Y second;

    Pair(X first,Y second){
        this.first=first;
        this.second=second;
    }

    public X getX() {
        return first;
    }

    public void setX(X first) {
        this.first = first;
    }

    public Y getY() {
        return second;
    }

    public void setY(Y second) {
        this.second = second;
    }


    public Comparator<Pair<X, Y>> getComparator(){
        return new Comparator<Pair<X, Y>>() {

            @Override
            public int compare(Pair<X, Y> o1, Pair<X, Y> o2) {
                double a=(Double) o1.getY();
                double b=(Double) o2.getY();
                if(a==b){
                    return 0;
                }else if(a>b){
                    return 1;
                }else{
                    return -1;
                }
            }
        };
    }

}

public class Main{

    public static void main(String[] arg){

        List<Pair<String,Double>> val1 = new ArrayList<Pair<String,Double>>();

        val1.add(new Pair<String,Double>("f\u2081(1) = ", 0.1));
        val1.add(new Pair<String,Double>("f\u2082(1) = ", 0.2));
        val1.add(new Pair<String,Double>("f\u2083(1) = ", 0.1));
        val1.add(new Pair<String,Double>("f\u2084(1) = ", 1.1));
        val1.add(new Pair<String,Double>("f\u2085(1) = ", 1.2));
        val1.add(new Pair<String,Double>("f\u2086(1) = ", 2.0));
        val1.add(new Pair<String,Double>("f\u2087(1) = ", 2.1));
        val1.add(new Pair<String,Double>("f\u2088(1) = ", 0.3));

        Collections.sort(val1,new Pair<String,Double>("",0.0).getComparator());

        for (Pair<String, Double> pair : val1) {
            System.out.println(pair.getX()+" "+pair.getY());
        }
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.