Help comparing float member variables using Comparators
I am able to compare Strings fine, but would like to know how I can rank floating point numbers?
getChange() returns a String. I want to be able to sort descending. How can I do this?
UPDATE:
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 how开发者_Go百科ever, not sure what you want
if (change2 > change2) return 1;
}
}
I am getting the compile time error:
This method must return a result of type int ChangeComparator.java
How about this:
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);
}
}
Note that Java 1.4 introduced Float#compare(float, float)
(and an equivalent in Double
), which can be pretty much used directly:
public class ChangeComparator implements Comparator<Quote>
{
public int compare(Quote o1, Quote o2) {
return Float.compare(o1.getChange(), o2.getChange());
}
}
(After editing, I notice that @BorislavGizdov has mentioned this in his answer already.)
Also worth noting that Java 8 Comparator#comparing(...)
and Comparator#comparingDouble(...)
provide a straightforward way of constructing these comparators directly.
Comparator<Quote> changeComparator = Comparator.comparing(Quote::getChange);
Will compare using boxed Float
values.
Comparator<Quote> changeComparator = Comparator.comparingDouble(Quote::getChange);
Will compare using float
values promoted to double
values.
Given that there is no Comparator#comparingFloat(...)
, my preference would be to use the comparingDouble(...)
method, as this only involves primitive type conversion, rather than boxing.
Read the javadoc of Comparator#compare()
method.
Compares its two arguments for order. Returns a negative integer, zero or a positive integer as the first argument is less than, equal to or greater than the second.
So, basically:
float change1 = o1.getChange();
float change2 = o2.getChange();
if (change1 < change2) return -1;
if (change1 > change2) return 1;
return 0;
Or if you like conditional operators:
return o1.getChange() < o2.getChange() ? -1
: o1.getChange() > o2.getChange() ? 1
: 0;
You however need to take account with Float.NaN
. I am not sure how you'd like to have them ordered. First? Last? Equally?
You can use Float.compare(float f1, float f2)
:
public static int compare(float f1, float f2)
Compares the two specified float values. Returns the value 0 if f1 is numerically equal to f2; a value less than 0 if f1 is numerically less than f2; and a value greater than 0 if f1 is numerically greater than f2.
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);
}
}
}
精彩评论