Have a string of "DateTimeTicks_Guid", use only CompareTo to generate find elements with a date range
I have a set of strings, the strings are in format: Ticks then an underscore than a Guid.
So for example: Ticks_Guid would be the string. The ticks are actually DateTime.MaxTicks - the ticks of 开发者_StackOverflow社区some date (aside: I do this to get the string to naturally show up in descending order).
Using only CompareTo is there a way to get the strings that are within a certain date range?
Since you say you have the strings in naturally descending order, I assume you have a fixed-width section for the ticks component of the string (otherwise the descending order bit won't work). The easiest comparison therefore would be to create two similarly formatted string; one for the tick value of the earlier date combined with an empty guid (all 0s) and another for the latter date combined with a guid that is all 0xFF, and then find the strings that sort between those in raw code-point order.
I'd just compare if the ticks are in between your dates to check in an if-statement:
ArrayList a = new ArrayList();
string b = "345678_lalala";
string c = "429817_lalelu";
a.Add(b);
a.Add(c);
//date to check
string begin = "298918";
string end = "419812";
ArrayList outputList = new ArrayList();
foreach (string str in a)
{
if (str.CompareTo(begin) == 1 && str.CompareTo(end) == -1)
{
outputList.Add(str);
}
}
精彩评论