Need help w/ArrayList Sort

James

Regular
Joined
Oct 3, 2002
Messages
78
Can someone help me. I can not get an ArrayList to sort I get the error message "At least one object must implement IComparable." I know that I can only add a single item to an Arraylist using the Add method so I created a class that will take two items. My arraylist works fine but I cannot get it to sort now.

I am trying to select items from one listbox and moving them over to another listbox. When the items move to the other listbox I populate an arraylist with all the listbox items and sort so the other listbox will be in alphabetic order. The reason I need to populate the Arraylist with two items is because the value and text properties of the listbox are different. I want bothtext and value properties in the second listbox after they are moved.

My code is below.

Thanks,

James

<%@ Register TagPrefix="uc1" TagName="version" Src="version.ascx" %>
<%@ Register TagPrefix="uc1" TagName="WyleLogo" Src="WyleLogo.ascx" %>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="ReadOnly.aspx.vb" Inherits="WylePricingSystem._ReadOnly"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>ReadOnly</title>
<meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR">
<meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<uc1:wylelogo id="WyleLogo1" runat="server"></uc1:wylelogo>
<TABLE width="75%" align="center" border="0">
<tr>
<td align="middle">
<h1><font color="#006464">Pricing Tool Search Engine</font>
</h1>
</td>
</tr>
</TABLE>
<hr>
<BR>
<TABLE width="50%" align="center" border="1">
<TR>
<TD align="middle"><STRONG>Task Order</STRONG></TD>
<TD align="middle"><asp:label id="twp" runat="server" ForeColor="Blue">Label</asp:label></TD>
</TR>
<TR>
<TD align="middle"><STRONG>WBS</STRONG></TD>
<TD align="middle"><asp:label id="wbs" runat="server" ForeColor="Blue">Label</asp:label></TD>
</TR>
<TR>
<TD align="middle"><STRONG>Work Package</STRONG></TD>
<TD align="middle"><asp:label id="WP" runat="server" ForeColor="Blue">Label</asp:label></TD>
</TR>
</TABLE>
<br>
<asp:customvalidator id="CustomValidator1" style="Z-INDEX: 101; LEFT: 188px; POSITION: absolute; TOP: 361px" runat="server" OnServerValidate="CustomValidator1_ServerValidate" Display="None"></asp:customvalidator>
<table align="center" border="0">
<tr>
<td rowSpan="2"><asp:listbox id="Name1" runat="server" Rows="10" SelectionMode="Multiple" Width="162px"></asp:listbox></td>
<TD align="middle" width="45"><asp:button id="Button1" runat="server" Text=">>"></asp:button><asp:button id="Button2" runat="server" Text="<<"></asp:button></TD>
<td rowSpan="2"><asp:listbox id="Name2" runat="server" Rows="10" SelectionMode="Multiple" Width="162px"></asp:listbox></td>
</tr>
<tr>
</tr>
</table>
<br>
<TABLE align="center" border="1">
<TR>
<TD align="middle"><input id="back" onclick="history.go(-1);" type="button" value="Back"></TD>
<TD><asp:button id="home" runat="server" Text="Home" CausesValidation="False"></asp:button><FONT face="Arial Black"></FONT></TD>
<TD><asp:button id="search" runat="server" Text="Search" CausesValidation="False"></asp:button><FONT face="Arial Black"></FONT></TD>
<TD><asp:button id="saveIt" runat="server" Text="Save"></asp:button><FONT face="Arial Black"></FONT></TD>
<TD><asp:button id="close" runat="server" Text="Close" CausesValidation="False"></asp:button><FONT face="Arial Black"></FONT></TD>
</TR>
</TABLE>
<div align="right"><uc1:version id="Version1" runat="server"></uc1:version></div>
</form>
</body>
</HTML>



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If IsValid Then
Dim s, x, countUser As Integer
countUser = Name1.Items.Count - 1

Dim ind As New ArrayList()

For s = 0 To countUser

If Name1.Items(s).Selected = True Then
Name2.Items.Add(New ListItem(Name1.SelectedItem.Text, Name1.SelectedItem.Value))
ind.Add(s)
Name1.Items(s).Selected = False
End If
Next s
x = ind.Count - 1

For s = x To 0 Step -1
Name1.Items.Remove(Name1.Items(ind(s)))
Next s

Dim aList As New ArrayList()
Dim k As Integer

For k = 0 To Name2.Items.Count - 1
aList.Add(New listNameItems(Name2.Items(k).Text, Name2.Items(k).Value))
Next

aList.Sort()
Name2.Items.Clear()

For k = 0 To aList.Count() - 1
Name2.Items.Add(New ListItem(aList.Item(k).listitems.listtext, aList.Item(k).listitems.listvalue))
Next

End If
End Sub

Public Class listNameItems
Public listText As String
Public listValue As String

Public Sub New(ByVal text As String, ByVal value As String)
MyBase.new()
listText = text
listValue = value
End Sub
End Class
 
You need to implement IComparable in your listNameItems class.

Visual Basic:
Public Class listNameItems
	Implements System.IComparable

	Public Overloads Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
		'Compare object passed to current object
	End Function

	Public listText As String
	Public listValue As String

	Public Sub New(ByVal text As String, ByVal value As String)
		MyBase.new()
		listText = text
		listValue = value
	End Sub
End Class

[mshelp]ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemIComparableClassCompareToTopic.htm[/mshelp]
 
Back
Top