12

我可以很好地比较字符串,但想知道如何对浮点数进行排名?

getChange() 返回一个字符串。我希望能够降序排序。我怎样才能做到这一点?

更新:

package org.stocktwits.helper;

import java.util.Comparator;

import org.stocktwits.model.Quote;

public class ChangeComparator implements Comparator<Quote>
{
    public int compare(Quote o1, Quote o2) {
        float change1 = Float.valueOf(o1.getChange());
        float change2 = Float.valueOf(o2.getChange());

        if (change1 < change2) return -1;
        if (change1 == change2) return 0; // Fails on NaN however, not sure what you want
        if (change2 > change2) return 1;
    }
}

我收到编译时错误:

This method must return a result of type int    ChangeComparator.java   
4

4 回答 4

18

这个怎么样:

public class ChangeComparator implements Comparator<Quote>
{
    public int compare(Quote o1, Quote o2) {
        Float change1 = Float.valueOf(o1.getChange());
        Float change2 = Float.valueOf(o2.getChange());
        return change1.compareTo(change2);
    }
}

请注意,Java 1.4 引入了Float#compare(float, float)(以及 中的等价物Double),几乎可以直接使用:

public class ChangeComparator implements Comparator<Quote>
{
    public int compare(Quote o1, Quote o2) {
        return Float.compare(o1.getChange(), o2.getChange());
    }
}

(编辑后,我注意到@BorislavGizdov 已经在他的回答中提到了这一点。)


另外值得注意的是,Java 8Comparator#comparing(...)提供Comparator#comparingDouble(...)了一种直接构建这些比较器的直接方法。

Comparator<Quote> changeComparator = Comparator.comparing(Quote::getChange);

将使用装箱Float值进行比较。

Comparator<Quote> changeComparator = Comparator.comparingDouble(Quote::getChange);

将使用float提升为double值的值进行比较。

鉴于没有Comparator#comparingFloat(...),我的偏好是使用该comparingDouble(...)方法,因为这仅涉及原始类型转换,而不是装箱。

于 2010-09-14T00:32:48.533 回答
17

阅读Comparator#compare()方法的javadoc。

比较它的两个参数的顺序。返回负整数、零或正整数,因为第一个参数小于、等于或大于第二个。

所以,基本上

float change1 = o1.getChange();
float change2 = o2.getChange();
if (change1 < change2) return -1;
if (change1 > change2) return 1;
return 0;

或者,如果您喜欢条件运算符:

return o1.getChange() < o2.getChange() ? -1 
     : o1.getChange() > o2.getChange() ? 1 
     : 0;

但是,您需要考虑Float.NaN. 我不确定您希望如何订购它们。第一的?最后的?相等?

于 2010-09-14T00:07:11.283 回答
10

您可以使用Float.compare(float f1, float f2)

  public static int compare(float f1, float f2)

比较两个指定的浮点值。如果 f1 在数值上等于 f2,则返回值 0;如果 f1 在数值上小于 f2,则值小于 0;如果 f1 在数值上大于 f2,则值大于 0。

于 2014-06-02T21:50:25.127 回答
0
import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(System.in);
        int tc = input.nextInt();
        int alpha = 0;
        while (tc-- > 0) {
            int ttc = input.nextInt();
            int sort = input.nextInt();
            input.nextLine();
            Vector<student> v = new Vector<>();
            alpha++;
            while (ttc-- > 0) {
                String name = input.next();
                int weit = input.nextInt();
                int age = input.nextInt();
                float hight = input.nextFloat();

                v.add(new student(name, weit, age, hight));
            }
            Collections.sort(v);
            int count = 0;
            System.out.println("CENARIO {" + alpha + "}");
            for (student s : v) {
                System.out.print((count + 1) + " - ");
                System.out.println(s.name);
                count++;
                if (count == sort) {
                    break;
                }
            }
        }
    }

    private static class student implements Comparable<student> {

        String name;
        int weit;
        int age;
        float hight;

        public student(String name, int weit, int age, float hight) {
            this.name = name;
            this.weit = weit;
            this.age = age;
            this.hight = hight;
        }

        @Override
        public int compareTo(student t) {
            if (this.weit - t.weit != 0) {
                return t.weit - this.weit;
            }
            if (this.age - t.age != 0) {
                return this.age - t.age;
            }
            if (this.hight - t.hight != 0) {
                return Float.compare(this.hight, t.hight);
            }
            return this.name.compareTo(t.name);

        }
    }
}
于 2020-09-04T17:17:11.933 回答