Java List Sort sortMapEntryListByValue(List> entries, boolean descending)

Here you can find the source of sortMapEntryListByValue(List> entries, boolean descending)

Description

Sorts a List of Map.Entry into descending or ascending order by value

License

Apache License

Parameter

Parameter Description
entries a List of Map.Etnry objects
descending sort order

Declaration

public static void sortMapEntryListByValue(List<Map.Entry<String, Integer>> entries, boolean descending) 

Method Source Code

//package com.java2s;
/*// ww w. ja  va  2 s.c  o m
   Copyright (C) 2005-2012, by the President and Fellows of Harvard College.
    
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
    
 http://www.apache.org/licenses/LICENSE-2.0
    
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
    
   Dataverse Network - A web application to share, preserve and analyze research data.
   Developed at the Institute for Quantitative Social Science, Harvard University.
   Version 3.0.
*/

import java.util.*;

public class Main {
    /**
     * Sorts a List of Map.Entry into descending order by value
     *
     * @param entries   a List of Map.Etnry objects
     */
    public static void sortMapEntryListByValue(List<Map.Entry<String, Integer>> entries) {
        sortMapEntryListByValue(entries, true);
    }

    /**
     * Sorts a List of Map.Entry into descending or ascending order by value
     *
     * @param entries       a List of Map.Etnry objects
     * @param descending    sort order
     */
    public static void sortMapEntryListByValue(List<Map.Entry<String, Integer>> entries, boolean descending) {
        final int order = descending ? -1 : 1;
        Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() {
            public int compare(Map.Entry<String, Integer> e1, Map.Entry<String, Integer> e2) {
                Integer e1Value = e1.getValue();
                Integer e2Value = e2.getValue();
                return order * (e1Value.compareTo(e2Value));
            }

            public boolean equals(Object obj) {
                return super.equals(obj);
            }
        });
        //return entries;
    }
}

Related

  1. sortListStingArrayByFirstDes( List list)
  2. sortLocales(final List locales)
  3. sortLongSeqList(List firstList, List secondList)
  4. sortLoops(ArrayList list)
  5. sortMap(Map targetMap, List sortedList)
  6. sortMessages(List ms)
  7. sortNames(List nameLs, String splitStr)
  8. sortRepositorynames(List list)
  9. sortSimpleName(List list)