Analytics.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. namespace CompanyCoreLib
  2. {
  3. public class Analytics
  4. {
  5. public List<DateTime> PopularMonths(List<DateTime> dates)
  6. {
  7. var dateTimeCounterDictionary = new Dictionary<DateTime, int>();
  8. int previousYear = DateTime.Now.Year - 1;
  9. foreach (DateTime iterDate in dates)
  10. {
  11. if (iterDate.Year == previousYear)
  12. {
  13. // вычисляем начало месяца для текущей даты
  14. var dateMonthStart = new DateTime(
  15. iterDate.Year, // год
  16. iterDate.Month, // месяц
  17. 1); // день
  18. if (dateTimeCounterDictionary.ContainsKey(dateMonthStart))
  19. {
  20. dateTimeCounterDictionary[dateMonthStart]++;
  21. }
  22. else
  23. {
  24. dateTimeCounterDictionary[dateMonthStart] = 1;
  25. }
  26. }
  27. }
  28. return dateTimeCounterDictionary
  29. .OrderByDescending(pair => pair.Value)
  30. .ThenBy(pair => pair.Key)
  31. .Select(pair => pair.Key)
  32. .ToList();
  33. }
  34. }
  35. }