Skip to content

Instantly share code, notes, and snippets.

@h63542
Forked from JeffreyZhao/index.cs
Created August 18, 2012 01:22
Show Gist options
  • Save h63542/3383798 to your computer and use it in GitHub Desktop.
Save h63542/3383798 to your computer and use it in GitHub Desktop.
// C#
List<string> keywords = ...;
var result = keywords
.GroupBy(k => k[0].ToUpper())
.ToDictionary(
g => g.Key,
g => g.OrderBy(k => k).ToList());
// 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
// 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);
}
// JavaScript(基于类库扩展)
var keywords = ...;
var result = keywords
.groupBy(function (k) { return k[0].toUpperCase(); })
.each(function (key, value) { value.sort(); });
# Ruby
keywords = ...
result = keywords \
.group_by { |k| k[0,1].upcase } \
.each { |key, value| value.sort! }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment