private List<ConfigurationItem> sortConfigurationItems(
      List<ConfigurationItem> selectedConfigurationItems) {
    final List<ConfigurationItem> sortedConfigurationItems =
        new ArrayList<>(selectedConfigurationItems);

    // Sort the list by last used profile
    if (selectedConfigItem != null && selectedConfigItem.getProfile() != null) {
      // If the same field is selected again - the last selected item should go on top
      boolean removedSelectedItem = sortedConfigurationItems.remove(selectedConfigItem);

      // Last used profile group should be on top
      Collections.sort(
          sortedConfigurationItems,
          new Comparator<ConfigurationItem>() {
            @Override
            public int compare(ConfigurationItem lhs, ConfigurationItem rhs) {
              if (lhs.getProfile() != null
                  && lhs.getProfile().equalsIgnoreCase(selectedConfigItem.getProfile())
                  && rhs.getProfile() != null
                  && !rhs.getProfile().equals(selectedConfigItem.getProfile())) {
                return -1;
              } else {
                return 0;
              }
            }
          });

      // Put the last selected item on top
      if (removedSelectedItem) {
        sortedConfigurationItems.add(0, selectedConfigItem);
      }
    }
    return sortedConfigurationItems;
  }
 private void selectItemWithNextProfile() {
   if (sortedConfigurationItems == null
       || selectedConfigItem == null
       || selectedConfigItem.getProfile() == null
       || profiles == null
       || profiles.isEmpty()) {
     return;
   }
   selectedProfileIndex = (selectedProfileIndex + 1) % profiles.size();
   String selectedProfile = profiles.get(selectedProfileIndex);
   for (int i = 0; i < sortedConfigurationItems.size(); i++) {
     ConfigurationItem item = sortedConfigurationItems.get(i);
     if (item.getProfile() != null && item.getProfile().equals(selectedProfile)) {
       setSelectedConfigItem(item);
       break;
     }
   }
 }