The text of this article is not in this database — only its details are. Read it on the old site: Sorting MultiColumn ListView
2 Comments
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment.
The text of this article is not in this database — only its details are. Read it on the old site: Sorting MultiColumn ListView
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment.
Isaac HidalgoPosted Mar 28, 2011, 8:33 PM
This is a very interesting article, but im searching a method to sort multiple columns at the same time, I mean, like the Microsoft Excel advanced Sort dialog where you can choose which columns sort, which order and data type. Anyone has seen this already programmed and can share the example?. I'll program the sample and post it back. Meanwhile Im posting my personalized class to be able to choose the type of data, so you can sort values with money sign, numbers, strings and values with an ending percentage symbol. NOTE: variable names are typed in Spanish but is not difficult to translate them: numeros for numbers letras for strings porcentaje for percentage fechas for dates moneda for currency values ------------------------ Public Class Comparador Implements IComparer Private col As Integer Private order As SortOrder Public Enum Comparar numeros letras fechas moneda porcentaje End Enum Public Tipo_Comparacion As Comparar Private _formulario As Form Public Sub New(ByVal column As Integer, ByVal orden As SortOrder, ByVal tipo As Comparar) col = column Me.order = orden Tipo_Comparacion = tipo If IsNothing(_formulario.BackgroundImage) Then _formulario.BackgroundImage = My.Resources.rsRecursos.cacesa_logo _formulario.BackgroundImageLayout = ImageLayout.Center End If End Sub Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare Dim returnVal As Integer = -1 If col < CType(x, ListViewItem).SubItems.Count And col < CType(y, ListViewItem).SubItems.Count Then Select Case Tipo_Comparacion Case Comparar.letras If Trim(CType(x, ListViewItem).SubItems(col).Text) = String.Empty Then If Trim(CType(y, ListViewItem).SubItems(col).Text) = String.Empty Then returnVal = 0 Else returnVal = -1 End If Else If Trim(CType(y, ListViewItem).SubItems(col).Text) = String.Empty Then returnVal = 1 Else returnVal = [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text) End If End If Case Comparar.numeros If Trim(CType(x, ListViewItem).SubItems(col).Text) = String.Empty Then If Trim(CType(y, ListViewItem).SubItems(col).Text) = String.Empty Then returnVal = 0 Else returnVal = -1 End If Else If Trim(CType(y, ListViewItem).SubItems(col).Text) = String.Empty Then returnVal = 1 Else returnVal = Double.Parse(Trim(CType(x, ListViewItem).SubItems(col).Text)) - Double.Parse(Trim(CType(y, ListViewItem).SubItems(col).Text)) End If End If If returnVal <> 0 Then returnVal *= Math.Abs(1 / returnVal) Case Comparar.fechas If Trim(CType(x, ListViewItem).SubItems(col).Text) = String.Empty Then If Trim(CType(y, ListViewItem).SubItems(col).Text) = String.Empty Then returnVal = 0 Else returnVal = -1 End If Else If Trim(CType(y, ListViewItem).SubItems(col).Text) = String.Empty Then returnVal = 1 Else Dim fecha1 As DateTime = Date.Parse(CType(x, ListViewItem).SubItems(col).Text) Dim fecha2 As DateTime = Date.Parse(CType(y, ListViewItem).SubItems(col).Text) returnVal = DateDiff(DateInterval.Day, fecha1, fecha2) * -1 If returnVal <> 0 Then returnVal *= Math.Abs(1 / returnVal) End If End If Case Comparar.moneda If Trim(CType(x, ListViewItem).SubItems(col).Text) = String.Empty Then If Trim(CType(y, ListViewItem).SubItems(col).Text) = String.Empty Then returnVal = 0 Else returnVal = -1 End If Else If Trim(CType(y, ListViewItem).SubItems(col).Text) = String.Empty Then returnVal = 1 Else Dim uno As String = CType(x, ListViewItem).SubItems(col).Text Dim dos As String = CType(y, ListViewItem).SubItems(col).Text If uno.Substring(0, 1) = "-" Then uno = "$-" & uno.Substring(2) End If If dos.Substring(0, 1) = "-" Then dos = "$-" & dos.Substring(2) End If If uno.Substring(0, 1) = "$" Then uno = uno.Substring(1) End If If dos.Substring(0, 1) = "$" Then dos = dos.Substring(1) End If returnVal = Double.Parse(Trim(uno)) - Double.Parse(Trim(dos)) End If End If If returnVal <> 0 Then returnVal *= Math.Abs(1 / returnVal) Case Comparar.porcentaje If Trim(CType(x, ListViewItem).SubItems(col).Text) = String.Empty Then If Trim(CType(y, ListViewItem).SubItems(col).Text) = String.Empty Then returnVal = 0 Else returnVal = -1 End If Else If Trim(CType(y, ListViewItem).SubItems(col).Text) = String.Empty Then returnVal = 1 Else Dim uno As String = CType(x, ListViewItem).SubItems(col).Text Dim dos As String = CType(y, ListViewItem).SubItems(col).Text If uno.Substring(uno.Length - 1, 1) = "%" Then uno = uno.Substring(0, uno.Length - 1) End If If dos.Substring(dos.Length - 1, 1) = "%" Then dos = dos.Substring(0, dos.Length - 1) End If returnVal = Double.Parse(Trim(uno)) - Double.Parse(Trim(dos)) End If End If If returnVal <> 0 Then returnVal *= Math.Abs(1 / returnVal) End Select If order = SortOrder.Descending Then ' Invert the value returned by String.Compare. returnVal *= -1 End If End If Return returnVal End Function End Class
prema vidhyaPosted Oct 18, 2010, 7:06 AM
hi