Java-8: How to sort Map (based on values) using Map.Entry#comparingByValue while ignoring case sensitivity?

Viewed 1440

I want to sort a Map based on values alphabetically and ignoring case sensitivity and return the list of Keys.

 /**
   * This method will sort allCostPlanDetailsRows based on Value in <Key,Value> pair
   *
   * @param sortingOrder_ whether to sort the LinkedHashMap in Ascending order or Descending order
   * @return List<String> returns List of costPlanDetailsRowId in sorted order
   */
  private List<String> sortCostPlanDetailRows( SortingOrder sortingOrder_ )
  {
    return _allCostPlanDetailRows
      .entrySet()
      .stream()
      .sorted( sortingOrder_ == SortingOrder.DESC ? Map.Entry.<String, String>comparingByValue(Comparator.nullsFirst(Comparator.naturalOrder())).reversed() : Map.Entry.comparingByValue(Comparator.nullsFirst(
          Comparator.naturalOrder())) )
      .map( Map.Entry::getKey )
      .collect( Collectors.toList() );
  }

How can I achieve this?

Note: Suggestions are welcomed on if I can improve above piece of code.

1 Answers
Related