使用比较器帮助比较浮点成员变量
2022-09-05 00:02:41
我可以很好地比较字符串,但想知道如何对浮点数进行排名?
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