Java Map Sort sortBy(Collection collection, final Map valuesMap)

Here you can find the source of sortBy(Collection collection, final Map valuesMap)

Description

sort By

License

LGPL

Declaration

public static <T, C> Collection<T> sortBy(Collection<T> collection, final Map<T, C> valuesMap) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;

import java.util.List;
import java.util.Map;

public class Main {
    public static <T, C> Collection<T> sortBy(Collection<T> collection, final Map<T, C> valuesMap) {
        List<T> ordered = new ArrayList<T>(collection);
        Comparator<T> compartor = new Comparator<T>() {

            @SuppressWarnings("unchecked")
            @Override/*from ww w  .  j a va 2s  . c om*/
            public int compare(T a, T b) {
                C aValue = valuesMap.get(a);
                C bValue = valuesMap.get(b);
                return ((Comparable<C>) aValue).compareTo(bValue);
            }
        };

        Collections.sort(ordered, compartor);

        return ordered;
    }
}

Related

  1. sort(Map map)
  2. sort(Map map, Comparator> comparator)
  3. sort(Map map)
  4. sort(Map src)
  5. sort(T map, Comparator> c)
  6. sortByComparator(Map unsortMap)
  7. sortByComparator(Map unsortMap)
  8. sortByComparatorDecreasing(Map unsortMap)
  9. sortByComparatorDouble( final Map map)