简体   繁体   中英

Sort a list based on grouping on object fields, sort based on maximum in the group

I am trying to sort an arraylist based on groupings. Suppose I have a list of currency and cash USD 100, CAD 75, USD 10, EUR 80, USD 5

I want to sort the list based on maximum cash for a currency descending order, so required out should look like. USD 100, USD 10, USD 5, EUR 80, CAD 75.

I wrote below classes to achieve the same but no luck, can somebody help.

import java.math.BigDecimal;
import java.util.Comparator;

public class BO {

    String ccy;
    BigDecimal cash;


public String getCcy() {
    return ccy;
}


public void setCcy(String ccy) {
    this.ccy = ccy;
}


public BigDecimal getCash() {
    return cash;
}


public void setCash(BigDecimal cash) {
    this.cash = cash;
}


@Override
public String toString() {
    return "BO [ccy=" + ccy + ", cash=" + cash + "]";
}
public static final Comparator<BO> CUSTOM_SORTER = new Comparator<BO>() {
    @Override
    public int compare(BO o1, BO o2) {
        return o2.getCash().compareTo(o1.getCash());
    }
};

}

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class BODriver {

public static void main(String[] args) {
BO one = new BO();
one.setCcy("USD");
one.setCash(new BigDecimal(100));
BO two = new BO();
two.setCcy("CAD");
two.setCash(new BigDecimal(50));
BO three = new BO();
three.setCcy("USD");
three.setCash(new BigDecimal(10));
BO four = new BO();
four.setCcy("EUR");
four.setCash(new BigDecimal(70));
BO five = new BO();
five.setCcy("USD");
five.setCash(new BigDecimal(5));

List<BO> boList = new ArrayList<BO>();
boList.add(five);
boList.add(four);
boList.add(three);
boList.add(two);
boList.add(one);

System.out.println("Before sort");
System.out.println(boList);
Collections.sort(boList,BO.CUSTOM);
System.out.println("After sort");
System.out.println(boList);

}

}

也许您可以向连接 ccy+" "+cash.eg 的 BO 类添加一个字符串字段,您可以使用该字符串字段轻松地对您的对象进行排序。

public static final Comparator<BO> CUSTOM_SORTER = new Comparator<BO>() {
    @Override
    public int compare(BO o1, BO o2) {
       if(o2.getCcy().compareTo(o1.getCcy())==0)
         return o2.getCash().compareTo(o1.getCash());
       else
         return o2.getCcy().compareTo(o1.getCcy());
    }
};

try this as your comparator..

Here is a two step process.

First find max for each currency:

    Map<String, BigDecimal> maxGroup = boList.stream().collect( 
    Collectors.toMap(BO::getCcy, BO::getCash, BigDecimal::max) );

Then sort first by max cash value, then by currency and finally by cash value in each BO object:

    Comparator<BO> comparator = Comparator
    .<BO, BigDecimal>comparing( (bo) -> maxGroup.get( bo.getCcy() ) ).reversed() //first compare by max cash in desc order
    .thenComparing(BO::getCcy) //then by currency
    .thenComparing( Comparator.comparing(BO::getCash).reversed() ); //and finally by cash in desc order

    Collections.sort( boList, comparator); 

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