byRole function

A comparator creator which will set up a comparator which prioritizes participants who have a specific role.

Implementation

Comparator<CallParticipantState> byRole(List<String> roles) {
  return (CallParticipantState a, CallParticipantState b) {
    final aMatches = a.roles.fold(
      0,
      (matches, value) {
        if (roles.contains(value)) {
          return matches + 1;
        } else {
          return matches;
        }
      },
    );
    final bMatches = b.roles.fold(
      0,
      (matches, value) {
        if (roles.contains(value)) {
          return matches + 1;
        } else {
          return matches;
        }
      },
    );

    if (aMatches > bMatches) return -1;
    if (bMatches < aMatches) return 1;
    return 0;
  };
}