Kartia rework.
Contributed by flanagan.
This commit is contained in:
@ -29,11 +29,13 @@ import java.text.DecimalFormatSymbols;
|
||||
import java.text.NumberFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.l2jmobius.Config;
|
||||
import com.l2jmobius.commons.util.Rnd;
|
||||
@ -791,14 +793,24 @@ public final class Util
|
||||
}
|
||||
|
||||
/**
|
||||
* Short an <L2PcInstance, Integer> map by its integer values.
|
||||
* @param unsortedMap
|
||||
* @return
|
||||
* This will sort a Map according to the values. Default sort direction is ascending.
|
||||
* @param <K> keyType
|
||||
* @param <V> valueType
|
||||
* @param map Map to be sorted.
|
||||
* @param descending If you want to sort descending.
|
||||
* @return A new Map sorted by the values.
|
||||
*/
|
||||
public static Map<L2PcInstance, Integer> sortByValue(Map<L2PcInstance, Integer> unsortedMap)
|
||||
public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map, boolean descending)
|
||||
{
|
||||
Map<L2PcInstance, Integer> sortedMap = new TreeMap<>(new ValueComparator(unsortedMap));
|
||||
sortedMap.putAll(unsortedMap);
|
||||
return sortedMap;
|
||||
if (descending)
|
||||
{
|
||||
return map.entrySet().stream().sorted(Map.Entry.comparingByValue(Collections.reverseOrder())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
|
||||
}
|
||||
return map.entrySet().stream().sorted(Map.Entry.comparingByValue()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
|
||||
}
|
||||
|
||||
public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map)
|
||||
{
|
||||
return map.entrySet().stream().sorted(Map.Entry.comparingByValue()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
|
||||
}
|
||||
}
|
||||
|
@ -1,46 +0,0 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.util;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Mobius
|
||||
*/
|
||||
public class ValueComparator implements Comparator<Object>
|
||||
{
|
||||
Map<?, ?> _map;
|
||||
|
||||
public ValueComparator(Map<?, ?> map)
|
||||
{
|
||||
_map = map;
|
||||
}
|
||||
|
||||
@SuppressWarnings(
|
||||
{
|
||||
"rawtypes",
|
||||
"unchecked"
|
||||
})
|
||||
@Override
|
||||
public int compare(Object keyA, Object keyB)
|
||||
{
|
||||
Comparable valueA = (Comparable<?>) _map.get(keyA);
|
||||
Comparable valueB = (Comparable) _map.get(keyB);
|
||||
return valueB.compareTo(valueA);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user