字符日期排序 10月23日10月5日10月7日10月12日10月26日代码排序,10月5日10月7日10月12日10月23日10月26日都是字符串的,求高人写的代码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这些数据存储到哪里的?数据库还是内存中。数据库里的话,存储的时候对于月和日总是存两位,然后orderby了内存中,用sortedlist,自定义comparer就可以了。 如果你能把你的日期格式统一下的话那我倒是有个办法。比如你的"10月5日"格式一下成:"10月05日"的话,那样把数据装到一datatable或集合中再排序就行了。 public class TestComparer : IComparer<string> { public int Compare(string x, string y) { if (x == null) { if (y == null) { return 0; } else { return -1; } } else { if (y == null) { return 1; } else { int retval = x.Length.CompareTo(y.Length);//这里根据自己需要再整理一下 if (retval != 0) { return retval; } else { return x.CompareTo(y); } } } } }使用 List<string> list = new List<string>();// { "10月23日", "10月5日", "10月7日", "10月12日", "10月26日" }; list.Add("10月23日"); list.Add("10月5日"); list.Add("10月7日"); list.Add("10月12日"); list.Add("10月26日"); list.Sort(new TestComparer()); private void button2_Click(object sender, EventArgs e) { List<String> listStr = new List<string>(); listStr.Add("10月23日"); listStr.Add("10月5日"); listStr.Add("10月7日"); listStr.Add("10月12日"); listStr.Add("10月26日"); listStr.Sort(new Comparison<string>(function)); } int function(string s1, string s2) { return DateTime.Compare(Convert.ToDateTime(s1), Convert.ToDateTime(s2)); } 是从数据库中读取出来之后加载到datalist中吗,那样的话,可以用sql语句,从表中读出,然后 order by Year("数据库中时间字段")desc,Month("数据库中时间字段")asc List<string> list = new List<string>(); list.Add("10月23日"); list.Add("10月5日"); list.Add("10月7日"); list.Add("10月12日"); list.Add("10月26日"); list.Sort(new Comparison<string>(delegate(string a, string b) { DateTime dt1 = DateTime.Parse(a); DateTime dt2 = DateTime.Parse(b); return dt1 < dt2 ? -1 : dt1 > dt2 ? 1 : 0; })); foreach (string s in list) Console.WriteLine(s);/*输出:10月5日10月7日10月12日10月23日10月26日*/ 楼上的方法好,我这里写了一个笨办法: static void Main(string[] args) { string[] array = new string[5]; array[0] = "10月23日"; array[1] = "10月5日"; array[2] = "10月7日"; array[3] = "10月12日"; array[4] = "10月26日"; string temp ; for (int i = 0; i < 5; i++) { int monthi = Convert.ToInt32(array[i].Substring(0, array[i].LastIndexOf('月'))); int dayi = Convert.ToInt32(array[i].Substring(array[i].LastIndexOf('月') + 1, array[i].LastIndexOf('日') - array[i].LastIndexOf('月') - 1)); for (int j = i + 1; j < 5; j++) { int monthj = Convert.ToInt32(array[j].Substring(0, array[j].LastIndexOf('月'))); int dayj = Convert.ToInt32(array[j].Substring(array[j].LastIndexOf('月') + 1, array[j].LastIndexOf('日') - array[j].LastIndexOf('月') - 1)); if (monthi > monthj) { temp = array[i]; array[i] = array[j]; array[j] = temp; } else if (monthi == monthj) { if (dayi < dayj) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } } for (int i = 0; i < 5; i++) { Console.WriteLine(array[i]); } Console.ReadKey(); }//输出为:10月26日10月23日10月12日10月7日10月5日 求大神。。。 C#中继承的一个小问题 C# 去掉小数位的问题。 javascript调用C#事件 如何使用对象调用显示窗口,不重复显示同一个窗口 3道面试题 关于购物网站的一个难点 怎樣直接打開一個流文件 在程序中添加了excel引用,在生成主输出的时候出现错误。这是什么原因呀?? 救命啊!怎么会出现这个问题? c# textbox控件 我要将它设置成 xp里面修改时间属性的那个textbox一样 应该如何设置 c#中Repeater取字段问题
数据库里的话,存储的时候对于月和日总是存两位,然后orderby了
内存中,用sortedlist,自定义comparer就可以了。
public class TestComparer : IComparer<string>
{
public int Compare(string x, string y)
{
if (x == null)
{
if (y == null)
{
return 0;
}
else
{
return -1;
}
}
else
{
if (y == null)
{
return 1;
}
else
{
int retval = x.Length.CompareTo(y.Length);//这里根据自己需要再整理一下 if (retval != 0)
{
return retval;
}
else
{
return x.CompareTo(y);
}
}
}
}
}使用 List<string> list = new List<string>();// { "10月23日", "10月5日", "10月7日", "10月12日", "10月26日" };
list.Add("10月23日");
list.Add("10月5日");
list.Add("10月7日");
list.Add("10月12日");
list.Add("10月26日"); list.Sort(new TestComparer());
{
List<String> listStr = new List<string>();
listStr.Add("10月23日");
listStr.Add("10月5日");
listStr.Add("10月7日");
listStr.Add("10月12日");
listStr.Add("10月26日"); listStr.Sort(new Comparison<string>(function));
} int function(string s1, string s2)
{
return DateTime.Compare(Convert.ToDateTime(s1), Convert.ToDateTime(s2));
}
List<string> list = new List<string>();
list.Add("10月23日");
list.Add("10月5日");
list.Add("10月7日");
list.Add("10月12日");
list.Add("10月26日");
list.Sort(new Comparison<string>(delegate(string a, string b)
{
DateTime dt1 = DateTime.Parse(a);
DateTime dt2 = DateTime.Parse(b);
return dt1 < dt2 ? -1 : dt1 > dt2 ? 1 : 0;
}));
foreach (string s in list)
Console.WriteLine(s);
/*
输出:
10月5日
10月7日
10月12日
10月23日
10月26日
*/
{
string[] array = new string[5];
array[0] = "10月23日";
array[1] = "10月5日";
array[2] = "10月7日";
array[3] = "10月12日";
array[4] = "10月26日";
string temp ;
for (int i = 0; i < 5; i++)
{
int monthi = Convert.ToInt32(array[i].Substring(0, array[i].LastIndexOf('月')));
int dayi = Convert.ToInt32(array[i].Substring(array[i].LastIndexOf('月') + 1, array[i].LastIndexOf('日') - array[i].LastIndexOf('月') - 1));
for (int j = i + 1; j < 5; j++)
{
int monthj = Convert.ToInt32(array[j].Substring(0, array[j].LastIndexOf('月')));
int dayj = Convert.ToInt32(array[j].Substring(array[j].LastIndexOf('月') + 1, array[j].LastIndexOf('日') - array[j].LastIndexOf('月') - 1));
if (monthi > monthj)
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
else if (monthi == monthj)
{
if (dayi < dayj)
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
for (int i = 0; i < 5; i++)
{
Console.WriteLine(array[i]);
}
Console.ReadKey();
}
//输出为:
10月26日
10月23日
10月12日
10月7日
10月5日