questo è un altro tentativo: gliela date una occhiata per vedere se va bene? :master:
codice:
Public Class HashtableInsertionOrder
Inherits DictionaryBase
Private _al As ArrayList
Public Sub New()
Me._al = New ArrayList()
End Sub
Public Sub Add(ByVal key As Object, ByVal value As Object)
If Not (Me.Dictionary.Contains(key)) Then
Me.Dictionary.Add(key, value)
Me._al.Add(key)
Else
Me.Dictionary("key") = value
End If
End Sub
Public Sub Remove(ByVal key As Object)
Me.Dictionary.Remove(key)
Me._al.Remove(key)
End Sub
Default Public Property Item(ByVal key As Object) As Object
Get
Return Me.Dictionary(key)
End Get
Set(ByVal value As Object)
Me.Dictionary(key) = value
End Set
End Property
Default Public Property Item(ByVal index As Integer) As DictionaryEntry
Get
Return New DictionaryEntry(Me._al(index), Me.Dictionary(Me._al(index)))
End Get
Set(ByVal value As DictionaryEntry)
Me.Dictionary(Me._al(index)) = value.Value
End Set
End Property
Public ReadOnly Property Keys() As Object()
Get
Return Me._al.ToArray()
End Get
End Property
Public ReadOnly Property Values() As Object()
Get
Dim o(Me._al.Count - 1) As Object
For i As Integer = 0 To Me._al.Count - 1
o(i) = Me.Dictionary(Me._al(i))
Next
Return o
End Get
End Property
Public Sub SortKeys()
Me._al.Sort()
End Sub
Public Sub SortValues()
Dim k As Object() = Me.Keys()
Dim v As Object() = Me.Values()
Array.Sort(v, k)
Me._al = New ArrayList(k)
End Sub
End Class
questo per il collaudo
codice:
Dim ht As New HashtableInsertionOrder()
ht.Add("stefano", 10)
ht.Add("carlo", 20)
ht.Add("nicola", 30)
ht.Add("pietro", 40)
ht.Add("lucia", 50)
PrintLn("non ordinato")
Response.Write("<hr />")
For Each de As DictionaryEntry In ht
PrintLn(de.Value, de.Key.ToString())
Next
PrintLn()
PrintLn("ordine di inserimento")
Response.Write("<hr />")
For i As Integer = 0 To ht.Count - 1
PrintLn(ht(i).Value, ht(i).Key.ToString)
Next
PrintLn()
PrintLn("ordine alfabetico delle chiavi")
Response.Write("<hr />")
ht.SortKeys()
For i As Integer = 0 To ht.Count - 1
PrintLn(ht(i).Value, ht(i).Key.ToString)
Next
PrintLn()
PrintLn("ordine alfabetico dei valori")
Response.Write("<hr />")
ht.SortValues()
For i As Integer = 0 To ht.Count - 1
PrintLn(ht(i).Value, ht(i).Key.ToString)
Next
Response.Write("<hr />")