简体   繁体   English

排序对象Java的ArrayList

[英]Sorting ArrayList of Objects Java

I have an ArrayList of Objects that I'd like to sort by their value. 我有一个对象的ArrayList ,我想按它们的值排序。 Basically I have 9 different mathematical functions (ie, f1(n) = log n , f2(n) = n , f3(n) = n log n , etc). 基本上我有9种不同的数学函数(即f1(n) = log nf2(n) = nf3(n) = n log n等)。 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. 我将值1插入到所有9个函数中,并将结果放在对象的ArrayList ,并附加了标签,如下面的代码所示。 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. 基本上,无论你在哪里看到log,funcValues,exponent,f4,squared等,这些都是计算数学函数答案的函数。 The output of this ArrayList is: 这个ArrayList的输出是:

f₁(1) = 0.0 f 1(1)= 0.0
f₂(1) = 1 f 2(1)= 1
f₃(1) = 1.0 f 3(1)= 1.0
f₄(1) = 0.0 f 4(1)= 0.0
f₅(1) = 1 f 5(1)= 1
f₆(1) = 1.0 f₆(1)= 1.0
f₇(1) = 2.0 f₇(1)= 2.0
f₈(1) = 1 f₈(1)= 1
f₉(1) = 0.0 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. 我被困在if语句中因为我不能做if(v1.getValue> v2.getValue)这样的事情,因为我使用了9个不同的函数调用来拉取每个值。

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。

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. map.values()将按排序顺序排列标签。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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