0

Good Morning!

I'm trying to compare a list by two fields, but the result is not working, can you help me?

I have an example list:

Name - MesesDuracao - Buy
A    - 1            - 10
A    - 2            - 5
B    - 1            - 8

I would like the ordering to look like this:

A-1-10
B-1-8
A-2-5

I'm trying this way:

Collections.sort (testImportados, new Comparator <BeanRelEstatisticaMateriaPrima> () {
    @Override
    public int compare (BeanRelEstatisticaMateriaPrima p1, BeanRelEstatisticaMateriaPrima p2)
    {    

        int comparison = p1.getMesesduration (). compareTo (p2.getMesesduration ());
        return comparison == 0? p1.getQtyBuy (). compareTo (p2.getQtyBuy ()): comparison;  

    }
});

However, it does only sorting by "getMesesduration ()", it is not sorting by quantity purchased.

Any suggestion?

Thank you

Geizon T.
  • 11
  • 5

4 Answers4

3
 Collections.sort(
     testImportados, 
     Comparator.comparing(BeanRelEstatisticaMateriaPrima::getMesesduration)
                     .thenComparing(BeanRelEstatisticaMateriaPrima::getQtyBuy));

Should be the simplest way to provide the correct Comparator, if you are using java-8 already.

Eugene
  • 117,005
  • 15
  • 201
  • 306
0

I did not understand your methods.

  • getQtyComprar() makes what?
  • getQtyBuy() makes what?

Two methods are different i think. But you are comparing two different method call in your comparator as p1.getQtyComprar().compareTo(p2.getQtyBuy ()). So your final Comparator should be in same method usage form.

If you wants to compare getQtyComprar after first comparing , code looks like ;

Collections.sort (testImportados, new Comparator <BeanRelEstatisticaMateriaPrima> () {
    @Override
    public int compare (BeanRelEstatisticaMateriaPrima p1, BeanRelEstatisticaMateriaPrima p2)
    {    

        int comparison = p1.getMesesduration (). compareTo (p2.getMesesduration ());
        return comparison == 0? p1.getQtyComprar (). compareTo (p2.getQtyComprar()): dateComparison;  

    }
});

If you wants to compare getQtyBuy after first comparing , code looks like ;

Collections.sort (testImportados, new Comparator <BeanRelEstatisticaMateriaPrima> () {
    @Override
    public int compare (BeanRelEstatisticaMateriaPrima p1, BeanRelEstatisticaMateriaPrima p2)
    {    

        int comparison = p1.getMesesduration().compareTo(p2.getMesesduration());
        return comparison == 0? p1.getQtyBuy().compareTo (p2.getQtyBuy()): dateComparison;  

    }
});

As a result of my post , chose your comparing method carefully.

drowny
  • 2,067
  • 11
  • 19
0

I think you want to first sort by quantity then by duration.

Collections.sort (testImportados, new Comparator <BeanRelEstatisticaMateriaPrima> () {
    @Override
    public int compare (BeanRelEstatisticaMateriaPrima p1, BeanRelEstatisticaMateriaPrima p2) {
        // first sort by quantity
        int cmp = Integer.compare(p1.getQtyBuy(), p2.getQtyBuy());

        // the quantities are the same, then sort by duration
        if (cmp == 0) {
            cmp = p1.getMesesDuration().compareTo(p2.getMesesDuration());
        }

        return cmp;
    }
pamcevoy
  • 1,136
  • 11
  • 15
0

I forgot to mention, I have negative values ​​in getQtyBuy and it looks like the code is getting lost.

I tried this other code:

public class CustomerSortingComparator implements Comparator < BeanRelEstatisticaMateriaPrima > {

  @Override
  public int compare(BeanRelEstatisticaMateriaPrima cust1, BeanRelEstatisticaMateriaPrima cust2) {

    // all comparison
    int compareMes = cust1.getMesesduration().compareTo(cust2.getMesesduration());
    int compareBuy = cust1.getQtyBuy().compareTo(cust2.getQtyBuy());
    int compareName = cust1.getProdname().compareTo(cust2.getProdname());

    // 3-level comparison using if-else block
    if (compareMes == 0) {
      return ((compareBuy == 0) ? compareName : compareBuy);
    } else {
      return compareMes;
    }
  }
}
Collections.sort(testImportados, new CustomerSortingComparator());

Continue sorting only by one parameter getMesesduration()

I do not understand.

Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
Geizon T.
  • 11
  • 5