@Override
        public int compare(ClientSession lhs, ClientSession rhs) {
            int comparison;
            final Long lhv, rhv;
            switch (sortField) {
                case SessionResultFilter.SORT_CREATION_DATE:
                    comparison = lhs.getCreationDate().compareTo(rhs.getCreationDate());
                    break;
                case SessionResultFilter.SORT_LAST_ACTIVITY_DATE:
                    lhv = snapshotsOfMutableData.computeIfAbsent(lhs.getAddress(), (jid) -> lhs.getLastActiveDate() == null ? 0 : lhs.getLastActiveDate().getTime());
                    rhv = snapshotsOfMutableData.computeIfAbsent(rhs.getAddress(), (jid) -> rhs.getLastActiveDate() == null ? 0 : rhs.getLastActiveDate().getTime());
                    comparison = lhv.compareTo(rhv);
                    break;
                case SessionResultFilter.SORT_NUM_CLIENT_PACKETS:
                    lhv = snapshotsOfMutableData.computeIfAbsent(lhs.getAddress(), (jid) -> lhs.getNumClientPackets());
                    rhv = snapshotsOfMutableData.computeIfAbsent(rhs.getAddress(), (jid) -> rhs.getNumClientPackets());
                    comparison = Long.compare(lhv, rhv);
                    break;
                case SessionResultFilter.SORT_NUM_SERVER_PACKETS:
                    lhv = snapshotsOfMutableData.computeIfAbsent(lhs.getAddress(), (jid) -> lhs.getNumServerPackets());
                    rhv = snapshotsOfMutableData.computeIfAbsent(rhs.getAddress(), (jid) -> rhs.getNumServerPackets());
                    comparison = Long.compare(lhv, rhv);
                    break;
                case SessionResultFilter.SORT_USER:
                    // sort first by name, then by resource
                    String lUsername = lhs.isAnonymousUser() ? "" : lhs.getAddress().getNode();
                    String rUsername = rhs.isAnonymousUser() ? "" : rhs.getAddress().getNode();
                    comparison = compareString(lUsername, rUsername);
                    if (comparison == 0) {
                        comparison = compareString(lhs.getAddress().getResource(),
                                rhs.getAddress().getResource());
                    }
                    break;
                default:
                    comparison = 0;
            }
            if (sortOrder == SessionResultFilter.DESCENDING) {
                comparison *= -1; // Naturally ascending, flip sign if descending
            }
            return comparison;
        }
Compares sessions according to sort fields.

Instances of this comparator retain a snapshot of the state of objects it is comparing. An instance of this
comparator should therefor be discarded after use.

@author Iain Shigeoka
xmppserver/src/main/java/org/jivesoftware/openfire/SessionResultFilter.java