Skip to main content
1 of 3
sfdcfox
  • 501.7k
  • 21
  • 473
  • 828

Your best bet is to write a wrapper for this.

public class AddressWrapper implements Comparable {
    public Address address;
    public Integer counter;
    public AddressWrapper(Address address, Integer counter) {
        this.address = address;
        this.counter = counter;
    }
    public Integer compareTo(Object other) {
        return counter-((AddressWrapper)other).counter;
    }
}

Which you would then populate:

AddressWrapper[] addressList = new AddressWrapper[0];
for(Address key: addresses.keyset()) {
    addresses.add(new AddressWrapper(key, addresses.get(key));
}
addressList.sort();
// addressList[0].address contains the most used address
sfdcfox
  • 501.7k
  • 21
  • 473
  • 828