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<...>LinkedHashMapstatic

さて、あなたの解決策はとても良いです。これらの余分なポイントを実現するには、もう少しコードが必要です。

必要なことを実行するコードを次に示します。

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 に答える