Many articles have been written to Hashtable have internal sorting mechanism, if you want to customize the order to write their own algorithm to achieve: it sounds very abstract, I always like simple and practical things, I will summarize how to achieve a custom sort hashtable.
First look at the basic implementation of the common Hashtable:
public static void Main ()
{
Hashtable ht = new Hashtable ();
Ht. ADD ("Key1", "value1");
Ht. ADD ("Key2", "value2");
Ht. ADD ("Key3", "value3");
Ht. ADD ("Key4", "value4");
Ht. ADD ("Key5", "value5");
foreach (String str in HT. Keys)
{
Console.WriteLine (str + ":" + ht[str]);
}
}
Results of the operation:
The reason for this result is known to all that hashtable internal sorting mechanism dictates.
Let me say the following types of sorting and implementations in peacetime:
First, in what order do I add in order to output:
public class Nosorthashtable:hashtable
{
Private ArrayList list = new ArrayList ();
public override void Add (object key, Object value)
{
Base. ADD (key, value);
List. ADD (key);
}
public override void Clear ()
{
Base. Clear ();
List. Clear ();
}
public override void Remove (object key)
{
Base. Remove (key);
List. Remove (key);
}
public override ICollection Keys
{
Get
{
return list;
}
}
Note here: ArrayList are unordered (the order in which they are added is the order of the output). Does it have to be combined with Hashtable to achieve this function? This inherits the rich function that Hashtable has Hashtable, and satisfies the function that ArrayList does not sort. To meet our requirements.
public static void Main ()
{
nosorthashtable ht = new nosorthashtable ();
Ht. ADD ("Key1", "value1");
Ht. ADD ("Key2", "value2");
Ht. ADD ("Key3", "value3");
Ht. ADD ("Key4", "value4");
Ht. ADD ("Key5", "value5");
foreach (String str in HT. Keys)
{
Console.WriteLine (str + ":" + ht[str]);
}
}
This operation will meet my requirements:
It worked!
Second, I sort by the size order of the Hashtable medium key
is actually sorted by the ASCII value of each character on the line. Compares the ASCII value of each character from left to right until the value of the ASCII that satisfies the two characters is different that stops the comparison
public static void Main ()
{
Hashtable ht = new Hashtable ();
Ht. ADD ("ee", "value1");
Ht. ADD ("DD", "value2");
Ht. ADD ("CC", "Value3");
Ht. ADD ("BB", "value4");
Ht. ADD ("AA", "Value5");
ArrayList list = new ArrayList (HT. Keys);
List. Sort ();
foreach (string str in list)
{
Console.WriteLine (str+ ":" +ht[str]);
}
}
Operating effect:
It worked!
Three, I press Hashtable in the worth of the size on the line sort
The same principle: in fact, the ASCII value of each character is sorted by the line. Compares the ASCII value of each character from left to right until the value of the ASCII that satisfies the two characters is different that stops the comparison
public static void Main ()
{
Hashtable ht = new Hashtable ();
Ht. ADD ("A", "3");
Ht. ADD ("B", "4");
Ht. ADD ("C", "2");
Ht. ADD ("D", "1");
ArrayList list = new ArrayList (HT. Values);
List. Sort ();
foreach (string svalue in list)
{
IDictionaryEnumerator IDE = ht. GetEnumerator ();
while (IDE. MoveNext ())
{
if (IDE. value.tostring () = = svalue)
{
Console.WriteLine (IDE. Key + ":" + svalue);
}
}
}
}
Operating effect:
It worked!
For the second and third, we can see that the Hashtable key (keys) or value (values) is converted to ArrayList by the following method.
ArrayList list = new ArrayList (HT. Values);
ArrayList list= New ArrayList (HT. Keys);
This allows you to convert the sort of hashtable into a ArrayList order!
In addition ArrayList provides many ways to sort:
ArrayList.Sort ()-------------------Sort by the ASCII value of the character
ArrayList. Reverse ()---------------inverted array
And so on more ArrayList method. If you are not satisfied with the sorting function, then you have to ArrayList this array write algorithm can be ArrayList sort, ArrayList sort also completed Hashtable sort
Another point to note: The following method also supports the ordering of custom object types
List. Sort (IComparer comparer)
This is actually the embodiment of the policy pattern (strategy) in the. NET Framework class. Let me give you an example of its usage:
1. Declare a custom class:
public class Eployee
{
public string name;
public int age;
public string sex;
}
2. Instantiate 3 objects and put them into the ArrayList array.
Because the objects in the ArrayList array are sorted by age size. The default is not supported. So we need to define a sort of algorithm class to implement the IComparer interface to redefine the sorting algorithm. That is, the dynamic modification algorithm (policy mode) is implemented.
public static void Main ()
{
Eployee EP1 = new Eployee ();
Ep1.name = "Charles";
Ep1.age = 21;
Ep1.sex = "male";
Eployee EP2 = new Eployee ();
Ep2.name = "Sun";
Ep2.age = 43;
Ep2.sex = "male";
Eployee ep3 = new Eployee ();
Ep3.name = "Sunny";
Ep3.age = 18;
Ep3.sex = "male";
ArrayList employeelist = new ArrayList ();
Employeelist.add (EP1);
Employeelist.add (EP2);
Employeelist.add (EP3);
Employeelist.sort (New Myemployeecompare ());
}
The classes that implement the IComparer interface Myemployeecompare as follows:
public class Myemployeecompare:icomparer
{
IComparer Members
}
}
This makes it possible to sort by age size from small to large.
A final note:
For the special requirements of Hashtable ordering, it is possible to convert its key or value into ArrayList, order for ArrayList, and then implement the Hashtable sort (the second above, the third embodiment of this). After all, ArrayList already supports some sort of ordering, and it also supports the sorting of custom types (strategy), and, of course, the ability to write your own algorithms for sorting.
Several methods of "turn" hashtable sorting