Created
July 31, 2011 07:49
-
-
Save JeffreyZhao/1116547 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// C# | |
List<string> keywords = ...; | |
var result = keywords | |
.GroupBy(k => k[0].ToUpper()) | |
.ToDictionary( | |
g => g.Key, | |
g => g.OrderBy(k => k).ToList()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// F# | |
let keywords = ... | |
let result = | |
keywords | |
|> Seq.groupBy (fun k -> k.[0].ToUpper()) | |
|> Seq.map (fun (k, v) -> (k, v |> Seq.sort |> List.ofSeq)) | |
|> Map.ofSeq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Java | |
List<String> keywords = ...; | |
Map<Character, List<String>> result = new HashMap<Character, List<String>>(); | |
for (String k: keywords) { | |
char firstChar = k.charAt(0); | |
if (!result.containsKey(firstChar)) { | |
result.put(firstChar, new ArrayList<String>()); | |
} | |
result.get(firstChar).add(k); | |
} | |
for (List<String> list: result.values()) { | |
Collections.sort(list); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// JavaScript(基于类库扩展) | |
var keywords = ...; | |
var result = keywords | |
.groupBy(function (k) { return k[0].toUpperCase(); }) | |
.each(function (key, value) { value.sort(); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ruby | |
keywords = ... | |
result = keywords \ | |
.group_by { |k| k[0,1].upcase } \ | |
.each { |key, value| value.sort! } |
Python 版的
keywords = ["dsaa","Asd","sadc","Gdfd","gdfdd","gaf","gabdddddd","eg"]
print dict([(i[0],list(i[1])) for i in groupby(sorted(keywords),lambda x:x[0].lower())])
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Scala
var keywords = List("dsaa","Asd","sadc","Gdfd","gdfdd","gaf","gabdddddd","eg")
keywords.groupBy(_.head.toUpperCase).mapValues((xs)=>xs.sort((x,y)=>x.toUpperCase.compare(y.toUpperCase)<0))