日期:2014-05-18  浏览次数:20738 次

一道小题求解!
输入电脑 cpu 内存 显卡 硬盘 光驱的价格
软后按照价格从贵到便宜输出;
利用2个数组,
一个string类型 元素为:cpu 内存 显卡 硬盘 光驱
另一个数组float 元素为所输入的价格
两个数组的下标一一对应
但是一对数组进行排序 下标就对不上了
用什么方法能解决此问题?

如果不用数组

CPU ="650";
memory = "90";
DISK ="550";
如何能把以上数据进行排序?



------解决方案--------------------
C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, Single> dic = new Dictionary<string, float>();
            dic.Add("CPU", 460);
            dic.Add("内存", 130);
            dic.Add("显卡", 720);
            dic.Add("硬盘", 680);
            dic.Add("光驱", 90);
            var result = from pairs in dic orderby pairs.Value select pairs;

            

            foreach (KeyValuePair<string, Single> pair in result)
            {
                Console.WriteLine("{0}\t---\t{1}", pair.Key, pair.Value);
            }

            Console.Read();
        }
    }
}