1234567891011121314151617181920212223242526272829303132333435363738 |
- namespace CompanyCoreLib
- {
- public class Analytics
- {
- public List<DateTime> PopularMonths(List<DateTime> dates)
- {
- var dateTimeCounterDictionary = new Dictionary<DateTime, int>();
- int previousYear = DateTime.Now.Year - 1;
- foreach (DateTime iterDate in dates)
- {
- if (iterDate.Year == previousYear)
- {
- // вычисляем начало месяца для текущей даты
- var dateMonthStart = new DateTime(
- iterDate.Year, // год
- iterDate.Month, // месяц
- 1); // день
- if (dateTimeCounterDictionary.ContainsKey(dateMonthStart))
- {
- dateTimeCounterDictionary[dateMonthStart]++;
- }
- else
- {
- dateTimeCounterDictionary[dateMonthStart] = 1;
- }
- }
- }
- return dateTimeCounterDictionary
- .OrderByDescending(pair => pair.Value)
- .ThenBy(pair => pair.Key)
- .Select(pair => pair.Key)
- .ToList();
- }
- }
- }
|