2012-08-27 42 views
0

可能重複:
Can I sort two lists in relation to each other?排序於類別

我有一個排序方案,我不知道如何acheive呢? 兩個收藏品

List<<String>String> Key, List<<String>String> Value 我必須對a-z的值進行排序,並且不應更改相應值的密鑰。

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

任何人請幫幫我嗎?

+1

發生了什麼?2 => two'? – stracktracer

+0

和排序後不按字母順序排序... 8 =>一,0 =>九? – jelies

+0

我的清單是分開的。不在一個集合中。即密鑰和價值或與索引相匹配的獨立集合。我必須對這些值進行排序,以便重新排序。 – Prabhu

回答

0

使用具有text關鍵樹形圖(「二」,「四大」),我們可以按字母順序,後來再次獲得兩個列表:

// 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()); 

希望這有助於。

0

如何使用TreeMap?你有兩個相應條目的元組,你可以按照你的意願對它們進行排序。