Sort List box in ASP .Net

SortedList :

Represents a collection of key/value pairs that are sorted by the keys and are accessible by key and by index.


Code Behind :
    Partial Class SortListBox
        Inherits System.Web.UI.Page

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                'Let List box Having the following Item.
                Dim strcountry() As String = {"Denmark", "China", "Italy", "Australia", "India", "Egypt", "Bhutan"}
                Dim strcountryID() As String = {"Den", "Chi", "Ita", "Aus", "Ind", "Egy", "Bhu"}

                'Populate the List Box
                For i As Int16 = 0 To strcountry.Length - 1
                    ListBox1.Items.Add(i)
                    ListBox1.Items(i).Text = strcountry(i)
                    ListBox1.Items(i).Value = strcountryID(i)
                Next
            End If
        End Sub

        'Click on btnSort to Sort the ListBox.
        Protected Sub btnSort_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSort.Click

            'Use the object of SortedList to Sort the Items
            Dim objSortlst As New SortedList

            For i As Int16 = 0 To ListBox1.Items.Count - 1
                'objSortlst.Add(key,value) . It keeps value in sorted Order
                objSortlst.Add(ListBox1.Items(i).Text, ListBox1.Items(i).Value)
            Next

            'Now objSortlst is having data in Sorted order of Key
            ListBox1.DataSource = objSortlst
            ListBox1.DataTextField = "key"
            ListBox1.DataValueField = "value"
            ListBox1.DataBind()
        End Sub

    End Class

Use of Globalization Culture ,CultureInfo and CultureTypes

Globalization is the process of designing and developing applications that function for multiple cultures.
CultureInfo : Provides information about a specific culture (called a "locale" for unmanaged code development). The information includes the names for the culture, the writing system, the calendar used, and formatting for dates and sort strings.

CultureTypes : Defines the types of culture lists that can be retrieved using
System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes)


Use the Following code in aspx (design page).Controls Used : Calender, Label and List box (AutoPostback=true).


Code Behind :
    Partial Class frmGlobalizationCulture
        Inherits System.Web.UI.Page

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Try
                If Not IsPostBack Then
                    'Bind all the Cultures in List box on First time page load.
                    lstCulture.Items.Clear()
                    Dim CulInfo As System.Globalization.CultureInfo()
                    CulInfo = System.Globalization.CultureInfo.GetCultures(Globalization.CultureTypes.SpecificCultures)
                    Dim StrShort As New SortedList 'Used for the Sorting the Values in ListBox
                    For j As Integer = 0 To CulInfo.Length - 1
                        StrShort.Add(CulInfo(j).DisplayName, CulInfo(j).ToString)
                    Next

                    'Bind all the "SpecificCultures" in the List Box.
                    lstCulture.DataSource = StrShort
                    lstCulture.DataTextField = "key"
                    lstCulture.DataValueField = "value"
                    lstCulture.DataBind()

                    'Select the "CurrentCulture" in the List Box.
                    lstCulture.SelectedValue = System.Threading.Thread.CurrentThread.CurrentCulture.ToString()
                End If
            Catch ex As Exception
                Throw ex
            End Try
        End Sub

        Protected Sub lstCulture_SelectedIndexChanged(ByVal sender As  Object, ByVal e As System.EventArgs) Handles  lstCulture.SelectedIndexChanged
            Try
                'Set the Selected Culture in the ListBox as the CurrentCulture.
                System.Threading.Thread.CurrentThread.CurrentCulture =  New System.Globalization.CultureInfo(Me.lstCulture.SelectedValue)
                System.Threading.Thread.CurrentThread.CurrentUICulture =  New System.Globalization.CultureInfo(Me.lstCulture.SelectedValue)
                lstCulture.Focus()
            Catch ex As Exception
                Throw ex
            End Try
        End Sub

    End Class