简体   繁体   中英

Sorting ArrayList of Objects Java

I have an ArrayList of Objects that I'd like to sort by their value. Basically I have 9 different mathematical functions (ie, 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.

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

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

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, ie 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.

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());
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM