Ordering with partial explicit and then another order?

我与影子孤独终老i 提交于 2019-12-02 06:09:25

问题


What I need is to order a list in a custom way, I'm looking into the correct way and found guava's Ordering api but the thing is that the list I'm ordering is not always going to be the same, and I just need 2 fields to be at the top of the list, for example I have this:

List<AccountType> accountTypes = new ArrayList<>();
AccountType accountType = new AccountType();
accountType.type = "tfsa";
AccountType accountType2 = new AccountType();
accountType2.type = "rrsp";
AccountType accountType3 = new AccountType();
accountType3.type = "personal";
accountTypes.add(accountType3);
accountTypes.add(accountType2);
accountTypes.add(accountType);
//The order I might have is : ["personal", "rrsp", "tfsa"]
//The order I need is first "rrsp" then "tfsa" then anything else

I tried with a custom comparator and using Ordering in Guava library, something like this:

public static class SupportedAccountsComparator implements Comparator<AccountType> {
    Ordering<String> ordering = Ordering.explicit(ImmutableList.of("rrsp", "tfsa"));
    @Override
    public int compare(AccountType o1, AccountType o2) {
        return ordering.compare(o1.type, o2.type);
    }
}

but it throws an exception because explicit ordering doesnt support other items that are not in the list you provided, is there a way to do a partial explicit ordering? something like:

Ordering.explicit(ImmutableList.of("rrsp", "tfsa")).anythingElseWhatever();

回答1:


You don't need Guava for this, everything you need is in the Collections API.

Assuming AccountType implements Comparable, you can just provide a Comparator that returns minimum values for "tfsa" and "rrsp", but leaves the rest of the sorting to AccountType's default comparator:

Comparator<AccountType> comparator = (o1, o2) -> {
    if(Objects.equals(o1.type, "rrsp")) return -1;
    else if(Objects.equals(o2.type, "rrsp")) return 1;
    else if(Objects.equals(o1.type, "tfsa")) return -1;
    else if(Objects.equals(o2.type, "tfsa")) return 1;
    else return o1.compareTo(o2);
};
accountTypes.sort(comparator);

If you don't want your other items sorted, just provide a default comparator that always returns 0.




回答2:


Here's a Comparator solution that uses a List of strings to represent your sorting order. Change your sorting order by merely changing the order of the strings in your sortOrder list.

  Comparator<AccountType> accountTypeComparator = (at1, at2) -> {
    List<String> sortOrder = Arrays.asList(
        "rrsp",
        "tfsa",
        "third"
        );
    int i1 = sortOrder.contains(at1.type) ? sortOrder.indexOf(at1.type) : sortOrder.size();
    int i2 = sortOrder.contains(at2.type) ? sortOrder.indexOf(at2.type) : sortOrder.size();
    return i1 - i2;
  };
  accountTypes.sort(accountTypeComparator);


来源:https://stackoverflow.com/questions/42125743/ordering-with-partial-explicit-and-then-another-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!