Value based sorting on key value collection

Animesh
Posted by in ASP.NET category on for Beginner level | Views : 7459 red flag
Rating: 5 out of 5  
 1 vote(s)

Guys has anybody faced problems with the Key, value pair collections not sorting the collections based on values . As all the collections sort themself on keys . As i needed a collection of Key,Value pairs which does sorting on values rather than key, For that reason i have created my own collection class which dose sorting on the values. And this is working perfectly for me .

''' <summary>

''' This class behaves as a normal list

''' but the sorting happens on value not on keys

''' </summary>

Public Class ValueBasedSortableList

REM List of all the items of Type ITEM

Private container As New Generic.List(Of Item)

Private _count As Integer = 0


Private _order As Integer = 1

Public Enum Order

Ascending = 1

Descending = 2

End Enum

Public ReadOnly Property Count() As Integer

Get

Return _count

End Get


End Property

Public ReadOnly Property Items() As Generic.List(Of Item)

Get

Return container

End Get

End Property

''' <summary>

''' Adds item in the collection


''' </summary>

''' <param name="Key">Key</param>

''' <param name="Value">Value</param>

''' <remarks></remarks>


Public Sub Add(ByVal Key As String, ByVal Value As String)

Dim objItem As New Item(Key, Value)

Item.Sortorder = Order.Ascending

container.Add(objItem)

_count += 1

End Sub

Public Shadows Sub sort(Optional ByVal asd As Order = Order.Ascending)

Item.Sortorder = asd

container.Sort() 'New listCompare


End Sub

Public Class Item

Implements IComparable(Of Item)



Private _key As String

Private _value As String

Private Shared _order As Order = Order.Ascending

Public Shared Property Sortorder() As Order

Get


Return _order

End Get

Set(ByVal value As Order)

_order = value

End Set

End Property

Public Sub New(ByVal Key As String, ByVal Value As String)

_key = Key

_value = Value


End Sub

Public Property Key() As String

Get

Return _key

End Get

Set(ByVal value As String)

_key = value

End Set

End Property


Public Property Value() As String

Get

Return _value

End Get

Set(ByVal value As String)

_value = value

End Set

End Property

Public Function CompareTo(ByVal other As Item) As Integer Implements System.IComparable(Of Item).CompareTo


If _order = 1 Then

If Me.Value < CType(other, Item).Value Then

Return -1

ElseIf Me.Value = CType(other, Item).Value Then

Return 0

Else

Return 1

End If


ElseIf _order = 2 Then

If Me.Value > CType(other, Item).Value Then

Return -1

ElseIf Me.Value = CType(other, Item).Value Then

Return 0

Else

Return 1

End If


End If

End Function

End Class



End Class
Page copy protected against web site content infringement by Copyscape

About the Author

Animesh
Full Name: Aniemsh Misra
Member Level: Starter
Member Status: Member
Member Since: 6/24/2007 11:43:19 PM
Country: India



Login to vote for this post.

Comments or Responses

Posted by: Raja on: 6/27/2007
Nice article. Very much creative work.

Keep it up.
Posted by: Deysomnath on: 6/18/2015 | Points: 25
Useful Article Animesh.

just for curiosity, why can't you apply LINQ in collection to sort the collection by Value before you use ?

Login to post response

Comment using Facebook(Author doesn't get notification)