Here you can find the source of sortedByRankThenLength(Map
private static Map<String, int[]> sortedByRankThenLength(Map<String, int[]> map)
//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; } }