Java Map Sort sortedByRankThenLength(Map map)

Here you can find the source of sortedByRankThenLength(Map map)

Description

sorted By Rank Then Length

License

Open Source License

Declaration

private static Map<String, int[]> sortedByRankThenLength(Map<String, int[]> map) 

Method Source Code


//package com.java2s;
/*-/* w  w w .  ja  va 2 s  .co  m*/
 *******************************************************************************
 * Copyright (c) 2011, 2016 Diamond Light Source Ltd.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Matthew Gerring - initial API and implementation and/or initial documentation
 *******************************************************************************/

import java.util.Collections;
import java.util.Comparator;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Main {
    private static Map<String, int[]> sortedByRankThenLength(Map<String, int[]> map) {

        List<Entry<String, int[]>> ll = new LinkedList<Entry<String, int[]>>(map.entrySet());

        Collections.sort(ll, new Comparator<Entry<String, int[]>>() {

            @Override
            public int compare(Entry<String, int[]> o1, Entry<String, int[]> o2) {
                int val = Integer.compare(o2.getValue().length, o1.getValue().length);

                if (val == 0)
                    val = Integer.compare(o1.getKey().length(), o2.getKey().length());

                return val;
            }
        });

        Map<String, int[]> lhm = new LinkedHashMap<String, int[]>();

        for (Entry<String, int[]> e : ll)
            lhm.put(e.getKey(), e.getValue());

        return lhm;

    }
}

Related

  1. sortByValues(Map map)
  2. sortByValues(Map map)
  3. sortByValuesDesc(final Map map)
  4. sortClustersKeys(final Map> clusters)
  5. sortDoubleMap(Map map)
  6. sortedKeys(Map map)
  7. sortedMap(Map map, Comparator comparator)
  8. sortedString(Map c)
  9. sortedString(Map c)