hashtable排序

    按Hashtable中的值得大小就行排序 .
    原理同上:实际上是按照每一个字符的ASCII的值就行排序的。从左到右比较每个字符的Ascii的值,直到满足两个字符的ASCII的值不同即停止比较。


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;

  6. namespace HashtableSort
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Hashtable ht = new Hashtable();
  13.             ht.Add("aa", 88);
  14.             ht.Add("bb", 33);
  15.             ht.Add("cc", 33);
  16.             ht.Add("dd", 22);
  17.             ht.Add("ee", 11);
  18.             ht.Add("ff", 99);
  19.             ArrayList list = new ArrayList(ht.Values);

  20.             /* 转换成整数的排序 */
  21.             for (int i = 0; i list.Count; i++)
  22.                 list[i] = Convert.ToInt32(list[i]);

  23.             // Sort为升序排序
  24.             list.Sort();
  25.             // 将顺序反转
  26.             list.Reverse();

  27.             Hashtable tmpHT = new Hashtable();
  28.             foreach (int value in list)
  29.             {
  30.                 IDictionaryEnumerator ide = ht.GetEnumerator();
  31.                 while (ide.MoveNext())
  32.                 {
  33.                     if (Convert.ToInt32(ide.Value) == value && !tmpHT.ContainsKey(ide.Key))
  34.                     {
  35.                         Console.WriteLine(ide.Key + ":"+value);
  36.                         tmpHT.Add(ide.Key, ide.Value);
  37.                     }
  38.                 }
  39.             }
  40.             Console.ReadLine();
  41.         }
  42.     }
  43. }


image


参考文献:

http://blog.sina.com.cn/s/blog_5f120d690100odit.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值