Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//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
            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;
    }
}