我有一个对象列表,它有一个名为date的字符串属性。对象是从SQL数据库中填充的。date属性以MM-DD格式包含对象的月份和日期(无年份)。我希望能够以一种方式组织这些对象,使下一个日期最近的对象位于列表的首位。
我不使用DateTime对象或MySQL中的日期数据类型的原因是,每当日期在一年内超过时,我都试图阻止更新该年。我只想看看日期是来了还是已经过去了(很明显,如果日期已经过去了,它就会接近列表的底部)。
我知道这种想法可能并不理想,在这件事上,我愿意听取其他意见。
编辑:
下面是一些关于我所用内容的确切结构的更多信息:
样本数据库信息:
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 */
}一旦创建了对象,它们就存储在一个列表中,我希望根据当前最近的对象对它们进行排序。例如,如果今天对其进行排序,对象的顺序将是:
最终的目的是用这些信息填充一个表,这样我就可以知道哪个域更新日期即将到来,这样我就可以手动向客户发送一张发票。我不认为有必要加上这一年,因为每当确切日期过去时,我都要更新它。
再说一遍,我知道这可能不是最有效的方式来做这件事,并开放给所有的建议一个更好的实现,这只是我的思考过程。
发布于 2018-09-03 23:02:11
我想出了一些你可以针对你的问题修改的代码。但是,这可以变得更通用(而且更安全得多)。
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);
}
}这是输出:

发布于 2018-09-03 22:34:53
如果将它们作为字符串从数据库中读取,则可以尝试以下操作:
List<string> dates = GetDates(); //returns dates in form MM-DD from db
dates = dates.OrderBy(x=int.Parse(x.Split('-')[0])
.ThenBy(x=>int.Parse(x.Split('-')[1])
.ToList();
List<string> oldDates = new List<string>();
List<string> futureDates = new List<string>();
foreach(var date in dates)
{
string[] splitDate = date.Split('-');
int month = int.Parse(splitDate[0]);
int day = int.Parse(splitDate[1]);
if(month < DateTime.Today.Month)
oldDates.Add(date);
else if(month == DateTime.Today.Month)
{
if(day < DateTime.Today.Day)
oldDates.Add(date);
else
futureDates.Add(date)(
}
else
futureDates.Add(date);
dates = futureDates.AddRange(oldDates);
}我在手机上,所以如果可以的话请格式化:)
发布于 2018-09-03 22:42:40
您可以将这三个属性添加到类中,然后按此顺序排序?
internal class Domain
{
private string name;
private string renewDate;
private string client;
/* Getters, Setters, Constructors, & Other Methods */
public int LastRenewalDateMonth => int.Parse(renewDate.Split('-')[0]);
public int LastRenewalDateDay => int.Parse(renewDate.Split('-')[1]);
public DateTime LastRenewalDate => new DateTime(1901, LastRenewalDateMonth, LastRenewalDateDay);
}你不关心今年,就像你说的。
希望这能有所帮助。
https://stackoverflow.com/questions/52156564
复制相似问题