关闭

asp.net 键值队排序

12476人阅读 评论(0) 收藏 举报
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<string, string> ball = new Dictionary<string, string> {   
                                               { "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<string, string> 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 < keyList.Length - 1; 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("</br>");
            for (int k = keyList.Length - 2; k >= 0; k--)
            {
                if (k == keyList.Length - 2)
                {
                    Response.Write("最晚时间:" + keyList[k] + "  最晚时间对应的价格:" + valueList[k]+"元");
                    Response.Write("</br>");
                }
                if(k==0)
                {
                    Response.Write("最早时间:" + keyList[k] + "  最早时间对应的价格:" + valueList[k]+"元");
                }
            }

            //Response.Write(keyList[k] + "," + valueList[k] + "$");
            //Response.Write(keyList[k] + " " + valueList[k] + " ");

            
        }


        
    }
}


 

 

效果图

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:635527次
    • 积分:8137
    • 等级:
    • 排名:第2477名
    • 原创:168篇
    • 转载:148篇
    • 译文:0篇
    • 评论:74条
    最新评论