2012-08-27 41 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?你有两个相应条目的元组,你可以按照你的意愿对它们进行排序。