Results 1 to 4 of 4

Thread: Need help w/ArrayList Sort

  1. #1

    Thread Starter
    Registered User
    Join Date
    Jul 2002
    Posts
    80

    Question Need help w/ArrayList Sort

    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

  2. #2
    Addicted Member
    Join Date
    Apr 2002
    Posts
    235
    You have to implement a Compare method in your class. I'm working on an example and will post it as soon as I know it's working.

  3. #3
    Addicted Member
    Join Date
    Apr 2002
    Posts
    235
    ok, here is the code for your class. Once you make these changes, the sort should work. For information about what I did, poke around VB.NET tutorials and help files about interfaces.

    VB Code:
    1. Public Class listNameItems
    2.     Implements IComparable
    3.  
    4.     Public listText As String
    5.     Public listValue As String
    6.  
    7.     Public Sub New(ByVal text As String, ByVal value As String)
    8.         MyBase.new()
    9.         listText = text
    10.         listValue = value
    11.     End Sub
    12.  
    13.     Function CompareTo(ByVal x As Object) As Integer Implements IComparable.CompareTo
    14.         Return Me.listText.CompareTo(x.listText)
    15.     End Function
    16. End Class

  4. #4

    Thread Starter
    Registered User
    Join Date
    Jul 2002
    Posts
    80
    Thanks csynder,

    I did something a little different. But I'll use your code next time since it less typing.

    Public Structure DataElement
    Public PKey As String
    Public Data As String
    Public Sub New(ByVal PKeyIn As String, ByVal DataIn As String)
    PKey = PKeyIn
    Data = DataIn
    End Sub
    End Structure

    Public Class DataElementComparer
    Implements IComparer

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
    Return String.Compare(DirectCast(x, DataElement).PKey, DirectCast(y, DataElement).PKey)
    End Function
    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width