Live Chat Icon For mobile
Live Chat Icon

How to sort ArrayList

Platform: ASP.NET| Category: Collections and Lists

<asp:Button id='btnAsc' runat='server' Text='Asc'></asp:Button>
<asp:ListBox id='ListBox1' runat='server'></asp:ListBox>
<asp:Button id='btnDesc' runat='server' Text='Desc'></asp:Button>

VB.NET


Private Sub Page_Load(sender As Object, e As System.EventArgs)
   ’ Put user code to initialize the page here
   	Dim arrlist As New ArrayList()
   	arrlist.Add('Tips')
   	arrlist.Add('Tricks')
   	arrlist.Add('Code')
   	arrlist.Add('Samples')
   	ListBox1.DataSource = arrlist
   	ListBox1.DataBind()
   	ViewState('AList') = arrlist
End Sub ’Page_Load


Private Sub btnAsc_Click(sender As Object, e As System.EventArgs)
   	CType(ViewState('AList'), ArrayList).Sort()
	ListBox1.DataSource = CType(ViewState('AList'), ArrayList)
	ListBox1.DataBind()
End Sub ’btnAsc_Click

Private Sub btnDesc_Click(sender As Object, e As System.EventArgs)
	CType(ViewState('AList'), ArrayList).Sort()
	CType(ViewState('AList'), ArrayList).Reverse()
	ListBox1.DataSource = CType(ViewState('AList'), ArrayList)
	ListBox1.DataBind()
End Sub ’btnDesc_Click

C#


private void Page_Load(object sender, System.EventArgs e)
{
	// Put user code to initialize the page here
	ArrayList arrlist = new ArrayList ();
	arrlist.Add ('Tips');
	arrlist.Add ('Tricks');
	arrlist.Add ('Code');
	arrlist.Add ('Samples');
	ListBox1.DataSource =arrlist ;
	ListBox1.DataBind ();
	ViewState['AList'] = arrlist ;
}

private void btnAsc_Click(object sender, System.EventArgs e)
{
	((ArrayList)ViewState['AList']).Sort() ;
	ListBox1.DataSource =(ArrayList)ViewState['AList'] ;
	ListBox1.DataBind ();
}

private void btnDesc_Click(object sender, System.EventArgs e)
{	
	((ArrayList)ViewState['AList']).Sort() ;
	((ArrayList)ViewState['AList']).Reverse ();
	ListBox1.DataSource =(ArrayList)ViewState['AList'] ;
	ListBox1.DataBind ();
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.