locked
sorting a list RRS feed

  • Question

  • User-81382370 posted

    Hi

    I have a class like this... 

        Private _Header As String
        Public Property Header() As String
            Get
                Return _Header
            End Get
            Set(ByVal value As String)
                _Header = value
            End Set
        End Property
    
    
        Private _Product As String
        Public Property Product() As String
            Get
                Return _Product
            End Get
            Set(ByVal value As String)
                _Product = value
            End Set
        End Property
     

     

    I would like to sort this list based on the Header value, how can I do that?
    Wednesday, November 12, 2008 8:22 AM

Answers

  • User1112064350 posted

    Hi

     if your .NET  version is .NET 3.0 or later you can use LINQ to Objects.

    if not , you can use List(Of T).Sort method by implementing IComparer(Of T) interface . something like following.

     

    Imports System.Collections.Generic
    
    Module Module1
    
        Sub Main()
            Dim list As List(Of Class1) = New List(Of Class1)()
            list.Add(New Class1("5", "aa"))
            list.Add(New Class1("2", "dd"))
            list.Add(New Class1("3", "cc"))
            list.Add(New Class1("4", "dd"))
            list.Add(New Class1("1", "aa"))
    
            list.Sort(New CompareClass1())
    
            For Each c As Class1 In list
                Console.WriteLine("Header:" + c.Header + " Product:" + c.Product)
            Next
            Console.ReadLine()
        End Sub
    
    End Module
    
    Public Class Class1
        Public Sub New(ByVal hd As String, ByVal pd As String)
            _Header = hd
            _Product = pd
        End Sub
    
        Private _Header As String
        Public Property Header() As String
            Get
                Return _Header
            End Get
            Set(ByVal value As String)
                _Header = value
            End Set
        End Property
    
    
        Private _Product As String
        Public Property Product() As String
            Get
                Return _Product
            End Get
            Set(ByVal value As String)
                _Product = value
            End Set
        End Property
    End Class
    
    Public Class CompareClass1
        Implements System.Collections.Generic.IComparer(Of Class1)
    
        Public Function Compare(ByVal x As Class1, ByVal y As Class1) As Integer Implements System.Collections.Generic.IComparer(Of Class1).Compare
            Return String.Compare(x.Header, y.Header)
        End Function
    End Class
     Hope it help.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 12, 2008 8:50 AM

All replies

  • User-690506545 posted

    That's just a class, it's not a list.

    You will have to implement a List Of (your object) and then you can sort it.

    Wednesday, November 12, 2008 8:33 AM
  • User-81382370 posted

    I do like this...

     

    Dim MySearchResult As New List(Of MyProductsHandler)

    MySearchResult.Add(New MyProductsHandler(m.Text, m.NId, m.PNId, TopText))

     

    Then I want to sort the list based on the Header which is the TopText value

    Wednesday, November 12, 2008 8:48 AM
  • User1112064350 posted

    Hi

     if your .NET  version is .NET 3.0 or later you can use LINQ to Objects.

    if not , you can use List(Of T).Sort method by implementing IComparer(Of T) interface . something like following.

     

    Imports System.Collections.Generic
    
    Module Module1
    
        Sub Main()
            Dim list As List(Of Class1) = New List(Of Class1)()
            list.Add(New Class1("5", "aa"))
            list.Add(New Class1("2", "dd"))
            list.Add(New Class1("3", "cc"))
            list.Add(New Class1("4", "dd"))
            list.Add(New Class1("1", "aa"))
    
            list.Sort(New CompareClass1())
    
            For Each c As Class1 In list
                Console.WriteLine("Header:" + c.Header + " Product:" + c.Product)
            Next
            Console.ReadLine()
        End Sub
    
    End Module
    
    Public Class Class1
        Public Sub New(ByVal hd As String, ByVal pd As String)
            _Header = hd
            _Product = pd
        End Sub
    
        Private _Header As String
        Public Property Header() As String
            Get
                Return _Header
            End Get
            Set(ByVal value As String)
                _Header = value
            End Set
        End Property
    
    
        Private _Product As String
        Public Property Product() As String
            Get
                Return _Product
            End Get
            Set(ByVal value As String)
                _Product = value
            End Set
        End Property
    End Class
    
    Public Class CompareClass1
        Implements System.Collections.Generic.IComparer(Of Class1)
    
        Public Function Compare(ByVal x As Class1, ByVal y As Class1) As Integer Implements System.Collections.Generic.IComparer(Of Class1).Compare
            Return String.Compare(x.Header, y.Header)
        End Function
    End Class
     Hope it help.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 12, 2008 8:50 AM
  • User-81382370 posted

    I do use LINQ, so I will use that. Thanks for your help

    Wednesday, November 12, 2008 9:00 AM