3

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();
2

2 Answers 2

7

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.

3
  • Will try this one, I had a Comparator like this but not that clean
    – Eefret
    Commented Feb 9, 2017 at 0:06
  • Oh I needed rrsp and then tfsa, but got tfsa then rrsp, I suppose changing the values will do
    – Eefret
    Commented Feb 9, 2017 at 0:11
  • Thanks that solved it, I had to drop the Objects.equals because I'm on android SDK 16, but it is working fine.
    – Eefret
    Commented Feb 9, 2017 at 0:18
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.