作者:幸福得味道2011_122 | 来源:互联网 | 2023-10-13 17:22
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.UI
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace jquerytest
{
public partial class WebForm33 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//声明键值队 (时间和价格)
Dictionary ball = new Dictionary {
{ "2012-3-30", "30.0" },
{ "2012-4-5", "10.5" },
{ "2012-4-10", "15.4" },
{ "2012-7-29", "20" },
{ "2012-4-6", "30" },
{ "2012-4-7", "50" }
};
ball.Add("2012-4-4", "60");//手动追加键值队
string key = "";
string value = "";
foreach (KeyValuePair kvp in ball)
{
key += kvp.Key + ",";
value += kvp.Value + ",";
}
string[] keyList = key.Split(',');
string[] valueList = value.Split(',');
string mytemp = "";
string tempValue = "";
for (int m = 0; m {//冒泡排序
for (int n = m - 1; n >= 0; n--)
{
if (Convert.ToDateTime(keyList[n]) > Convert.ToDateTime(keyList[n + 1]))
{//时间对比
//利用中间变量对比换位 (时间)
mytemp = keyList[n];
keyList[n] = keyList[n + 1];
keyList[n + 1] = mytemp;
tempValue = valueList[n];//价格
valueList[n] = valueList[n + 1];
valueList[n + 1] = tempValue;
}
}
}
//打印出排序后的结果
Response.Write("排序结果为:");
Response.Write("");
for (int k = keyList.Length - 2; k >= 0; k--)
{
if (k == keyList.Length - 2)
{
Response.Write("最晚时间:" + keyList[k] + " 最晚时间对应的价格:" + valueList[k]+"元");
Response.Write("");
}
if(k==0)
{
Response.Write("最早时间:" + keyList[k] + " 最早时间对应的价格:" + valueList[k]+"元");
}
}
//Response.Write(keyList[k] + "," + valueList[k] + "$");
//Response.Write(keyList[k] + " " + valueList[k] + " ");
}
}
}
效果图