如何遍历按字母顺序排列一个Hashtable的钥匙? [英] How to traverse keys of a Hashtable in alphabetical order?

查看:180
本文介绍了如何遍历按字母顺序排列一个Hashtable的钥匙?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是按字母升序遍历一个哈希表的键的最简单的方法是什么?

What is the easiest way to traverse a hashtable's keys in ascending alphabetical order?

推荐答案

这是相当依赖于什么关键的类型。但是让我们假设一分钟,他们都是字符串。您可以使用下面的LINQ查询

This is fairly dependent upon what the type of the key is. But lets assume for a minute that they are strings. You could use the following LINQ query

Hashtable table = GetHashTable();
var keys = table.Keys.Cast<String>().OrderBy(x => x);

有关更复杂的结构LINQ查询只是略有不同。让我们假设你有如下定义的一个关键

For more complex structures the LINQ query is only slightly different. Lets assume you had the following definition for a key

struct Name {
  public string First;
  public string Last;
  // Equality code omitted
}



该LINQ代码将是以下

The LINQ code would be the following

Hashtable table = GetHashtable();
var keys = table.Keys.Cast<Name>().OrderBy(x => x.First).ThenBy(x => x.Last);

这篇关于如何遍历按字母顺序排列一个Hashtable的钥匙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆