Comparator to sort arraylist object by float parameter

How can I create a comparator to be able to sort arraylist by float parameter?

Imagine that I have an object array object that has something like this:

ID=7 TFIDF=0.12654299 PR=25238.0 ID=4 TFIDF=0.12654299 PR=3638.0 ID=4 TFIDF=0.12654299 PR=3638.0 ID=4 TFIDF=0.12654299 PR=3638.0 ID=3 TFIDF=0.56442446 PR=14558.0 ID=1 TFIDF=0.0083091585 PR=3953.0 

I want to sort an array by TFIDF values. As far as I know, I can only sort them by integer. So im compares zeros.

So far I have this:

 Collections.sort(Contents_To_Show, new Comparator<ContentToShow>() { public int compare(ContentToShow o1, ContentToShow o2) { return (int) (o1.GetPR() - o2.GetPR()); } }); 

But. Again, he compares me only to the whole part. How can I compare the whole value?

Please, help.

thanks

+4
source share
4 answers

Follow-up on Pedro. If you want to sort by TFIDF, then PR,

 int result = Float.compare(o1.getTFIDF(), o2.getTFIDF()); if (result == 0) result = Float.compare(o1.getPR(), o2.getPR()); return result; 
+9
source

Well, from your comment, the problem arises that you are using a subtraction code for your comparator, but since the difference in the values ​​of the floats is less than 1 (absolute value), casting to int always rounded to 0 .

Use a more general form with if

  public int compare(ContentToShow o1, ContentToShow o2) { if (o1.getTFIDF() > o2.getTFIDF()) { return -1; } if (o1.getTFIDF() < o2.getTFIDF()) { return 1; } return 0 } 

As I said, how you get the value of the compare result (as long as it is coherent) is not related to sorting.

+3
source

I want to sort an array by TFIDF values.

Then why are you comparing PR values ​​with o1.GetPR()

 public int compare(ContentToShow o1, ContentToShow o2) { return (int) (o1.GetPR() - o2.GetPR()); } 

Try comparing TFIDF values ​​in your comparison method (but as mentioned, casting in int will always be rounded to int value ie 0)

 return (int) (o1.GetTFIDF() - o2.GetTFIDF()); 

So you can use

 return Float.valueOf(o1.GetTFIDF()).compareTo(Float.valueOf(o2.GetTFIDF())) 
+1
source

In Java 8 with lambda functions, it is now very easy to compare two floats in a list of objects. In this example, I use both lambda functions and functional operations (new functions in Java 8). Basically you can try this example. This example organizes people by floats that represent height:

 List<Person> people = Person.createStandardList(); // create a list with 2 standard users Collections.sort(people, (Person p1, Person p2) -> Float.compare(p1.height_in_meter, p2.height_in_meter)); people.stream().forEach((p) -> { // functional operation p.printName(); }); 

This is my Person class.

 public class Person { public static List<Person> createStandardList() { List<Person> people = new ArrayList<>(); Person p = new Person(); p.name = "Administrator"; p.surname = "admin"; p.address = "Via standard 1"; p.age = 26; p.sex = 'M'; p.height_in_meter = 1.70f; p.weight_in_kg = 68.50f; people.add(p); p = new Person(); p.name = "First"; p.surname = "Creator"; p.address = "Via standard 2"; p.age = 30; p.sex = 'F'; p.height_in_meter = 1.80f; p.weight_in_kg = 58.50f; people.add(p); p = new Person(); p.name = "Second"; p.surname = "Creator"; p.address = "Via standard 3"; p.age = 20; p.sex = 'F'; p.height_in_meter = 1.30f; p.weight_in_kg = 48.50f; people.add(p); return people; } public String name; public String surname; public String address; public int age; public char sex; public float height_in_meter; public float weight_in_kg; public String getName() { return name; } public String getSurname() { return surname; } public float getHeight_in_meter() { return high_in_meter; } public float getWeight_in_kg() { return weight_in_kg; } public void printName() { System.out.println(this.name); } } 

Output:

Second

Administrator

First

So, I think that in this example it is easier to understand the functions of JAVA 8. In the documentation you are trying to use a similar example, but with strings using a different method. ( Oracle's official documentation on lambda functions with an example collection )

+1
source

Source: https://habr.com/ru/post/1500058/


All Articles