Skip to content

Commit 3a11732

Browse files
committed
#221 Sort Categories alphabetically
1 parent 11ee54d commit 3a11732

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

Website/app_code/code/Blog.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -242,16 +242,12 @@ public static void SetConditionalGetHeaders(DateTime lastModified, HttpContextBa
242242

243243
public static Dictionary<string, int> GetCategories()
244244
{
245-
var result = new Dictionary<string, int>();
246-
247-
foreach (var category in GetVisiblePosts().SelectMany(post => post.Categories))
248-
{
249-
if (!result.ContainsKey(category))
250-
result.Add(category, 0);
245+
var result = GetVisiblePosts()
246+
.SelectMany(post => post.Categories)
247+
.GroupBy(category => category, (category, items) => new { Category = category, Count = items.Count() })
248+
.OrderBy(x => x.Category)
249+
.ToDictionary(x => x.Category, x => x.Count);
251250

252-
result[category] = result[category] + 1;
253-
}
254-
255251
return result;
256252
}
257253

0 commit comments

Comments
 (0)