简体   繁体   中英

How to sort list with two object fields?

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

 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.

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.

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;
    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM