1

我有一张地图,我想根据一些规则对其内容进行排序:

  1. 根据其值而不是其键按字母顺序(从 A 到 Z)对地图进行排序。
  2. 排序时忽略值的大小写敏感性。
  3. 考虑重复的单词(具有精确字母拼写和大小写的单词)。
  4. 将字母数字单词正确排序(Cbc2ee应该出现在Cbc100ee之前)。
  5. 处理非英文单词(área应该出现在以“a”字母开头的单词中,但实际上它出现在以“z”字母开头的单词之后,考虑 á 另一个字母)。

我想我想要的都是合乎逻辑的。我能够通过以下代码完成第 1、2 和 3 点:

public <K, V extends Comparable<? super V>> LinkedHashMap<K, V> sortMapByValues( Map<K, V> map ) {
    SortedSet<Map.Entry<K, V>> sortedEntries = new TreeSet<Map.Entry<K, V>>(
        new Comparator<Map.Entry<K, V>>() {
            @Override 
            public int compare( Map.Entry<K, V> e1, Map.Entry<K, V> e2 ) {
                String a = (String)e1.getValue();
                String b = (String)e2.getValue();

                int diff = a.compareToIgnoreCase( b );

                if (diff == 0) 
                    diff = a.compareTo(b);  

                return diff != 0 ? diff : 1;  // Special fix to preserve words with similar spelling.
            }
        }
    );

    sortedEntries.addAll( map.entrySet() );

    LinkedHashMap<K, V> sortedMap = new LinkedHashMap<K, V>();

    for( Map.Entry<K, V> sortedEntry : sortedEntries )
        sortedMap.put( sortedEntry.getKey(), sortedEntry.getValue() );

    return sortedMap;
}

第 (4) 点我找到了它的脚本,但我无法将它与我的代码合并: http ://www.davekoelle.com/alphanum.html

第 (5) 点我也找到了它的脚本,但我无法将它与我的代码合并: http ://www.javapractices.com/topic/TopicAction.do?Id=207

因为这些点会影响 compare(...) 方法。 任何人都可以帮助我吗?

4

1 回答 1

1

几点...

方法签名应该是:

public static <K, V> Map<K, V> sortMapByValues(Map<K, V> map)

注意: - 删除绑定到Comparable,因为您正在比较toString()映射值,而不是值本身 - 抽象类型的规范Map<...>,而不是具体的LinkedHashMap,根据良好的设计指南 -static因为它不会改变状态 - 它是“只是代码”

现在,您的解决方案非常好。它只需要更多的代码来实现这些额外的点。

这是一些应该做你想做的代码:

public static <K, V> Map<K, V> sortMapByValues(Map<K, V> map) {
    SortedSet<Map.Entry<K, V>> sortedEntries = new TreeSet<Map.Entry<K, V>>(new Comparator<Map.Entry<K, V>>() {
        public int compare(Map.Entry<K, V> e1, Map.Entry<K, V> e2) {
            String aRaw = e1.getValue().toString();
            String bRaw = e2.getValue().toString();
            String a = standardize(aRaw);
            String b = standardize(bRaw);

            // Check for hex
            try
            {
                Integer ai = Integer.parseInt(a, 16);
                Integer bi = Integer.parseInt(b, 16);
                int diff = ai.compareTo(bi);
                if (diff != 0)
                    return diff;
            }
            catch (NumberFormatException ignore)
            {
            }

            int diff = a.compareTo(b);
            if (diff != 0)
                return diff;
            return aRaw.compareTo(bRaw);
        }

        /** This method removes all accent (diacritic) marks and changes to lower case */
        String standardize(String input) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < input.length(); i++) {
                char c = input.charAt(i);
                sb.append(Normalizer.normalize(c + "", Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", ""));
            }
            return sb.toString().toLowerCase();
        }
    });

    // The rest is your code left as it was, cos it's fine
    sortedEntries.addAll(map.entrySet());

    LinkedHashMap<K, V> sortedMap = new LinkedHashMap<K, V>();

    for (Map.Entry<K, V> sortedEntry : sortedEntries)
        sortedMap.put(sortedEntry.getKey(), sortedEntry.getValue());

    return sortedMap;
}

这是测试代码:

public static void main(String[] args) {
    Map<String, String> map = new HashMap<String, String>();
    map.put("a", "CC96");
    map.put("b", "CC97");
    map.put("c", "CC102");
    map.put("d", "CC103");
    map.put("e", "aabbcc");
    map.put("f", "aabbCC");
    map.put("g", "atabends");
    map.put("h", "aaBBcc");
    map.put("i", "AABBcc");
    map.put("j", "aabbcc");
    map.put("k", "Cc102");
    map.put("l", "baldmöglichst");
    map.put("m", "bar");
    map.put("n", "barfuss");
    map.put("o", "barfuß");
    map.put("p", "spätabends");
    map.put("q", "ätabends");
    map.put("r", "azebra");

    System.out.println(sortMapByValues(map).toString()
        .replaceAll(", ", "\r").replaceAll("(\\{|\\})", ""));
}

输出:

a=CC96
b=CC97
c=CC102
k=Cc102
d=CC103
i=AABBcc
h=aaBBcc
f=aabbCC
e=aabbcc
g=atabends
q=ätabends
r=azebra
l=baldmöglichst
m=bar
n=barfuss
o=barfuß
p=spätabends
于 2011-12-26T03:47:56.643 回答