Hi how can i sort using Month
i have January, February.... December
i want to sort it out so the user will see it from January - December

my column name is "Months" and it is in subitems(0)

Thank you in a advanced

Recommended Answers

All 3 Replies

Try something like this:

Public Class Form1

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Dim sorter As New lvMonthSorter
      sorter.MonthColumn = 0 ' set the column that holds the month
      sorter.Order = SortOrder.Ascending
      ListView1.ListViewItemSorter = sorter
   End Sub

End Class


Public Class lvMonthSorter

   Implements System.Collections.IComparer
   ' array to get a month index from for sorting
   Private Shared Months() As String = {"january", "february", "march", "april", "may", "june", _
                           "july", "august", "september", "october", "november", "december"}

   Private _MonthColumn As Int32 = 0
      Public Property MonthColumn() As Int32
      Set(ByVal Value As Int32)
         _MonthColumn = Value
      End Set

      Get
         Return _MonthColumn
      End Get
   End Property

   Private _Order As SortOrder = SortOrder.Ascending
   Public Property Order() As SortOrder
      Set(ByVal Value As SortOrder)
         _Order = Value
      End Set
      Get
         Return _Order
      End Get
   End Property

   Public Function Compare(ByVal x As Object, ByVal y As Object) As Int32 Implements IComparer.Compare
      Dim result As Int32

      Dim xstr As String = CType(x, ListViewItem).SubItems(_MonthColumn).Text.ToLower
      Dim ystr As String = CType(y, ListViewItem).SubItems(_MonthColumn).Text.ToLower

      Dim xpos As Int32 = Array.FindIndex(Of String)(Months, Function(xs As String) xs = xstr)
      Dim ypos As Int32 = Array.FindIndex(Of String)(Months, Function(ys As String) ys = ystr)

      result = xpos.CompareTo(ypos)

      Select Case Order
         Case SortOrder.Ascending : Return result
         Case SortOrder.Descending : Return -result
         Case Else : Return 0
      End Select
   End Function

End Class
commented: This work for me. +2

Add another column (make the width=0) and populate it with the month numbers, then sort on that column. Or you could just add the items in month order to begin with. Or you could add your own custom sort routine for when the user clicks on the Month column header.

Great it works..
Thank you TnTinMN
and also thanks for the idea Reevrend Jim

you guys always help me thank you very much! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.