首页 > 解决方案 > 排序没有年份的日期

问题描述

我有一个对象列表,其中包含一个名为 date 的字符串属性。这些对象是从 SQL 数据库中填充的。date 属性包含 MM-DD 格式的对象 (NO YEAR) 的月份和日期。我希望能够以某种方式组织这些,以便下一个日期最接近当前日期的对象位于列表的顶部。

我不在 MySQL 中使用 DateTime 对象或 Date 数据类型的原因是因为我试图防止在一年内每次日期过去时更新年份。我只想能够查看日期是否即将到来或已经过去(显然,如果日期已经过去,它将在列表的底部)。

我知道这种思维方式可能并不理想,并且愿意接受有关此问题的其他建议。

编辑:

以下是我正在使用的确切结构的更多信息:

示例数据库信息:

DomainName       RenewDate       ClientName

mydomain.com     02-01           John Doe
thisdomain.com   08-30           Tim Jones
thatdomain.com   10-10           Jane Smith

对象:

internal class Domain
{
    private string name;
    private string renewDate;
    private string client;

/* Getters, Setters, Constructors, & Other Methods */
}

创建对象后,它们将存储在列表中,我想根据下一个最接近当前日期对它们进行排序。例如,如果今天对其进行排序,则对象的顺序将是:

最后的目的是用这些信息填充一个表格,这样我就知道哪些域续订日期即将到来,这样我就可以手动向客户发送发票。我认为没有必要添加年份,因为每次经过确切日期时我都必须更新它。

同样,我知道这可能不是解决此问题的最有效方法,并且愿意接受所有关于更好实施的建议,这只是我目前的过程。

标签: c#

解决方案


我想出了一些代码,您可以针对您的问题进行修改。然而,这可以更通用(也更安全|生产就绪)。

    class FancyDate
    {
        public int Month { get; set; }
        public int Day { get; set; }

        public FancyDate(string fancyDate)
        {
            var split = fancyDate.Split('-');
            Month = Int32.Parse(split[0]);
            Day = Int32.Parse(split[1]);
        }

        public override string ToString()
        {
            return $"{Month:D2}-{Day:D2}";
        }
    }

    class ClientRenewal
    {
        public string ClientName { get; set; }
        public FancyDate RenewalDate { get; set; } 
        public string DomainName { get; set; }

        public class ClientRenewalComparer : IComparer<ClientRenewal>
        {
            public int Compare(ClientRenewal x, ClientRenewal y)
            {
                if (x != null && y != null)
                    return String.Compare(x.RenewalDate.ToString(), y.RenewalDate.ToString(), StringComparison.Ordinal);
                throw new ArgumentNullException();
            }
        }

        public ClientRenewal(string clientName, string renewalDate, string domainName)
        {
            this.ClientName = clientName;
            this.RenewalDate = new FancyDate(renewalDate);
            this.DomainName = domainName;
        }

        public override string ToString()
        {
            return $"{DomainName} - {RenewalDate} - {ClientName}";
        }
    }
    class ClientRenewalList
    {
        private List<ClientRenewal> theClientList;

        public ClientRenewalList(List<ClientRenewal> list)
        {
            list.Sort(new ClientRenewal.ClientRenewalComparer());
            theClientList = new List<ClientRenewal>();
            foreach (var item in list)
            {
                theClientList.Add(item);
            }
        }

        private List<ClientRenewal> RotateListForCurrentDate()
        {
            // Bit of indirection to aid testing
            return RotateListForDate(DateTime.Now);
        }

        public List<ClientRenewal> RotateListForDate(DateTime dateTime)
        {
            var month = dateTime.Month;
            var day = dateTime.Day;
            int i = 0;
            while (i < theClientList.Count)
            {
                if (theClientList[i].RenewalDate.Month < month)
                {
                    i++;
                    continue;
                }
                if (theClientList[i].RenewalDate.Month == month)
                {
                    while (theClientList[i].RenewalDate.Day < day)
                    {
                        i++;
                    }
                }
                if (theClientList[i].RenewalDate.Month > month) break;
                if (theClientList[i].RenewalDate.Month >= month && theClientList[i].RenewalDate.Day >= day) break;
            }
            return theClientList.Skip(i).Concat(theClientList.Take(i)).ToList();
        }

        public List<ClientRenewal> GetListForDisplay()
        {
            return RotateListForCurrentDate();
        }
    }
    static void Main(string[] args)
    {

        //mydomain.com     02 - 01           John Doe
        //thisdomain.com   08 - 30           Tim Jones
        //thatdomain.com   10 - 10           Jane Smith
        var listOfClientRenewal = new List<ClientRenewal>()
        {
            new ClientRenewal("John Doe", "02-01", "mydomain.com"),
            new ClientRenewal("Tim Jones", "08-30", "thisdomain.com"),
            new ClientRenewal("Jane Smith", "10-10", "thatdomain.com")
        };
        var list = new ClientRenewalList(listOfClientRenewal).GetListForDisplay();
        foreach (var item in list)
        {
            Console.WriteLine(item);
        }
    }

这是输出:

输出


推荐阅读