Sorting an Arraylist of Objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Derek Martin

    Sorting an Arraylist of Objects

    Hi there, I've asked before but never got an answer that could get me
    completely to where I was heading. I have an arraylist that contains
    defined objects and I would like to sort this arraylist based on the values
    of one of the object's members.

    Here is a bit of code to illustrate my setup:

    Object construction and insertion:
    Dim entity As New entityobjects(e ntitynamenodeli st(i).InnerXml,
    typenodelist(i) .InnerXml, ...)
    entityarraylist .add(entity)
    ----------------------------------------------------------------------------------------------
    Object definition class:
    Public Class entityobjects
    Protected m_entityid As String
    Protected m_entityname As String
    Protected m_type As String
    ...

    Public Sub New(ByVal entityname As String, ByVal type As String ...)
    Me.entityname = entityname
    Me.type = type
    ...
    End Sub
    ----------------------------------------------------------------------------------------------

    Now, I would like to list out the objects sorted by entityname:

    'What can I put here to sort these???

    for each entity as object in entityarraylist
    richtextbox1.ap pendtext(entity .toString()) 'toString is overridden here
    in the object class
    next
    ----------------------------------------------------------------------------------------------

    I have read that I can implement icomparable or icomparer in the object
    class but can't quite put my finger on how?

    Thank you!
    Derek





    --
    Derek Martin


  • Scott Allen

    #2
    Re: Sorting an Arraylist of Objects

    Hi Derek:

    I have an article here that may be of some help:


    --
    Scott


    On Thu, 23 Dec 2004 09:41:39 -0600, "Derek Martin"
    <dmj2195@DONTSP AMMEokstateDOT. edu> wrote:
    [color=blue]
    >Hi there, I've asked before but never got an answer that could get me
    >completely to where I was heading. I have an arraylist that contains
    >defined objects and I would like to sort this arraylist based on the values
    >of one of the object's members.
    >
    >Here is a bit of code to illustrate my setup:
    >
    >Object construction and insertion:
    >Dim entity As New entityobjects(e ntitynamenodeli st(i).InnerXml,
    >typenodelist(i ).InnerXml, ...)
    >entityarraylis t.add(entity)
    >----------------------------------------------------------------------------------------------
    >Object definition class:
    >Public Class entityobjects
    > Protected m_entityid As String
    > Protected m_entityname As String
    > Protected m_type As String
    > ...
    >
    >Public Sub New(ByVal entityname As String, ByVal type As String ...)
    > Me.entityname = entityname
    > Me.type = type
    > ...
    >End Sub
    >----------------------------------------------------------------------------------------------
    >
    >Now, I would like to list out the objects sorted by entityname:
    >
    >'What can I put here to sort these???
    >
    >for each entity as object in entityarraylist
    > richtextbox1.ap pendtext(entity .toString()) 'toString is overridden here
    >in the object class
    >next
    >----------------------------------------------------------------------------------------------
    >
    >I have read that I can implement icomparable or icomparer in the object
    >class but can't quite put my finger on how?
    >
    >Thank you!
    >Derek[/color]

    Comment

    • Derek Martin

      #3
      Re: Sorting an Arraylist of Objects

      Thanks Scott, I will investigate!

      Derek



      "Scott Allen" <bitmask@[nospam].fred.net> wrote in message
      news:nlsls0tdor 749sc8cmibq668e ekc23g4ma@4ax.c om...[color=blue]
      > Hi Derek:
      >
      > I have an article here that may be of some help:
      > http://odetocode.com/Articles/203.aspx
      >
      > --
      > Scott
      > http://www.OdeToCode.com/blogs/scott/
      >
      > On Thu, 23 Dec 2004 09:41:39 -0600, "Derek Martin"
      > <dmj2195@DONTSP AMMEokstateDOT. edu> wrote:
      >[color=green]
      >>Hi there, I've asked before but never got an answer that could get me
      >>completely to where I was heading. I have an arraylist that contains
      >>defined objects and I would like to sort this arraylist based on the
      >>values
      >>of one of the object's members.
      >>
      >>Here is a bit of code to illustrate my setup:
      >>
      >>Object construction and insertion:
      >>Dim entity As New entityobjects(e ntitynamenodeli st(i).InnerXml,
      >>typenodelist( i).InnerXml, ...)
      >>entityarrayli st.add(entity)
      >>----------------------------------------------------------------------------------------------
      >>Object definition class:
      >>Public Class entityobjects
      >> Protected m_entityid As String
      >> Protected m_entityname As String
      >> Protected m_type As String
      >> ...
      >>
      >>Public Sub New(ByVal entityname As String, ByVal type As String ...)
      >> Me.entityname = entityname
      >> Me.type = type
      >> ...
      >>End Sub
      >>----------------------------------------------------------------------------------------------
      >>
      >>Now, I would like to list out the objects sorted by entityname:
      >>
      >>'What can I put here to sort these???
      >>
      >>for each entity as object in entityarraylist
      >> richtextbox1.ap pendtext(entity .toString()) 'toString is overridden
      >> here
      >>in the object class
      >>next
      >>----------------------------------------------------------------------------------------------
      >>
      >>I have read that I can implement icomparable or icomparer in the object
      >>class but can't quite put my finger on how?
      >>
      >>Thank you!
      >>Derek[/color]
      >[/color]


      Comment

      • Mythran

        #4
        Re: Sorting an Arraylist of Objects


        "Derek Martin" <dmj2195@DONTSP AMMEokstateDOT. edu> wrote in message
        news:%23aV5lXQ6 EHA.3908@TK2MSF TNGP12.phx.gbl. ..[color=blue]
        > Hi there, I've asked before but never got an answer that could get me
        > completely to where I was heading. I have an arraylist that contains
        > defined objects and I would like to sort this arraylist based on the
        > values of one of the object's members.
        >[/color]

        Hope the following Console application example helps :)


        Public Module Main
        <STAThread()> _
        Public Sub Main()
        Dim list As ArrayList = New ArrayList()
        list.Add(New Entity("abcdfz" ))
        list.Add(New Entity("bacdfz" ))
        list.Add(New Entity("zfdcba" ))
        list.Add(New Entity("fdadsf" ))
        list.Add(New Entity("akjjlk" ))
        list.Add(New Entity("ieiojd" ))
        list.Add(New Entity("adfklas jfdlkajs"))

        list.Sort()

        For Each obj As Entity In list
        Console.WriteLi ne(obj.EntityId )
        Next
        End Sub

        End Module

        Public Class Entity
        Implements IComparable

        Private mEntityId As String

        Public Property EntityId() As String
        Get
        Return mEntityId
        End Get
        Set
        mEntityId = Value
        End Set
        End Property

        Public Sub New(ByVal EntityId As String)
        mEntityId = EntityId
        End Sub

        Public Function CompareTo(ByVal obj As Object) As Integer Implements
        System.ICompara ble.CompareTo
        If TypeOf obj Is Entity
        Dim tmp As Entity = DirectCast(obj, Entity)
        Return mEntityId.Compa reTo(tmp.Entity Id)
        End If

        Throw New ArgumentExcepti on("The argument is not an Entity.")
        End Function
        End Class


        Comment

        Working...