Skip to main content
2 of 3
added 2 characters in body
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()) {
    addressList.add(new AddressWrapper(key, addresses.get(key));
}
addressList.sort();
// addressList[0].address contains the most used address
sfdcfox
  • 501.7k
  • 21
  • 473
  • 828