0

重複の可能性:
2 つのリストを相互に関連付けて並べ替えることはできますか?

ソートシナリオがありますが、それを達成する方法がわかりませんか? 2 つのコレクション

List<<String>String> Key, List<<String>String> Value az から値をソートする必要があり、対応する値のキーが変更されないようにする必要があります。

>>eg: Key and Value  
    2=>two  
    100=>four  
    1=>five  
    0=>nine  
    3=>eight  
    8=>one

>>after Sorting:
    3=>eight  
    1=>five  
    100=>four  
    0=>nine
    8=>one
    2=>two

誰でも私を助けてください?

4

3 に答える 3

1

テキストをキー ("two"、"four") としてツリーマップを使用すると、アルファベット順に並べて、後で 2 つのリストを再度取得できます。

// Key contains ("2", "100", "1", ...)
// Value contains ("two", "four", "five", ...)
SortedMap<String, String> result = new TreeMap<String, String>();

// assuming the same size for both lists
for (int i = 0; i < Key.size(); i++) {
    // when adding to TreeMap, keys are ordered automatically
    result.put(Value.get(i), Key.get(i));
}

List<String> orderedKey = new ArrayList<String>(result.keySet());
List<String> orderedValue = new ArrayList<String>(result.values());

お役に立てれば。

于 2012-08-27T07:30:51.867 に答える
0

TreeMapの使用はどうですか?対応する2つのエントリのタプルがあり、必要に応じてそれらを並べ替えることができます。

于 2012-08-27T07:31:24.153 に答える
0

情報を確認してください@http://paaloliver.wordpress.com/2006/01/24/sorting-maps-in-java/

于 2012-08-27T07:32:22.837 に答える