package org.wikiwebserver.util.comparator;

import java.util.Comparator;
import java.util.Map;

public class EntryKeyComparator<K, L> implements Comparator<Map.Entry<K, L>> {
    @SuppressWarnings("unchecked")
    public int compare(Map.Entry<K, L> obj1, Map.Entry<K, L> obj2) {
        Object key1 = ((Map.Entry<K, L>) obj1).getKey();
        Object key2 = ((Map.Entry<K, L>) obj2).getKey();
        if (key1.getClass() != key2.getClass()) {
            key1 = key1.toString();
            key2 = key2.toString();
        }        
        @SuppressWarnings("rawtypes")
		int diff = ((Comparable) key1).compareTo(key2);
        if (diff == 0) diff = 1; // Do not remove duplicates
        return diff;
    }
}

