Click here to Skip to main content
15,897,163 members
Articles / Web Development / ASP.NET

Sort generic collection

Rate me:
Please Sign up or sign in to vote.
4.00/5 (7 votes)
9 Apr 2008CPOL2 min read 44.7K   276   16  
This article describe how to sort generic collections
Imports System.Collections.Generic
Imports CollectionSorter
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub BtnSortAsc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSortAsc.Click
        Dim BookList As List(Of Book) = New List(Of Book)
        Dim m_book As Book
        m_book = New Book()
        m_book.Author = "Bill Gates"
        m_book.Title = "How to get rich as software engineer!"
        m_book.PublishDate = "1/1/2000"

        BookList.Add(m_book)
        m_book = New Book()
        m_book.Author = "Steve Job"
        m_book.Title = "Why apple went down the drain!"
        m_book.PublishDate = "1/1/2007"
        BookList.Add(m_book)

        Dim colsorter As CollectionSorter(Of Book) = New CollectionSorter(Of Book)("Author asc")
        BookList.Sort(colsorter)
        grdTest.DataSource = BookList
        grdTest.DataBind()
    End Sub

    Protected Sub btnSortDesc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSortDesc.Click
        Dim BookList As List(Of Book) = New List(Of Book)
        Dim m_book As Book
        m_book = New Book()
        m_book.Author = "Bill Gates"
        m_book.Title = "How to get rich as software engineer!"
        m_book.PublishDate = "1/1/2000"

        BookList.Add(m_book)
        m_book = New Book()
        m_book.Author = "Steve Job"
        m_book.Title = "Why apple went down the drain!"
        m_book.PublishDate = "1/1/2007"
        BookList.Add(m_book)

        Dim colsorter As CollectionSorter(Of Book) = New CollectionSorter(Of Book)("Author desc")
        BookList.Sort(colsorter)
        grdTest.DataSource = BookList
        grdTest.DataBind()
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Innovative Design
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions