Results 1 to 6 of 6

Thread: [RESOLVED] Sort an arraylist using compareTo

  1. #1
    Lively Member
    Join Date
    Jul 10
    Posts
    93

    Resolved [RESOLVED] Sort an arraylist using compareTo

    I have a class with the following function:

    Code:
     Public Function CompareTo(ByVal obj As Object) As Integer _
          Implements System.IComparable.CompareTo
            If Not TypeOf obj Is TicketData Then
                Throw New Exception("Object is not MyObject")
            End If
            Dim Compare As TicketData = CType(obj, TicketData)
            Dim result As Integer = Me.strSLA.CompareTo(Compare.strSLA)
    
            If result = 0 Then
                result = Me.strSLA.CompareTo(Compare.strSLA)
            End If
            Return result
    
        End Function

    Presort I get the following data:

    PRESORT
    6721.04
    3901.46
    2768.12
    2408.12
    795.42
    2377.29
    2145.21
    1866.25
    1775.83

    after running ticketList.Sort() I get:

    SORTED
    1.94
    102.15
    1180
    129.58
    13.54
    133.33
    139.17
    148.33
    1571.88

    So it appears that my function is comparing as if it were a string. Can someone help me understand what I'm missing?

  2. #2

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,501

    Re: Sort an arraylist using compareTo

    What's TicketData? A structure, a class, something more ordinary? What's strSLA?

  4. #4

  5. #5
    Lively Member
    Join Date
    Jul 10
    Posts
    93

    Re: Sort an arraylist using compareTo

    Sorry to not provide all of the details, ticketlist is an arraylist of an object class called ticektdata. Anyways I was trying to sort strSLA which was a string and I needed to convert ithe strSLA string to a decimal using CDec for the operation to work correctly. It works like a charm now, thank you!

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 02
    Posts
    21,655

    Re: [RESOLVED] Sort an arraylist using compareTo

    Well.... if it's a decimal value.... why is it being stored as a string?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    *Proof positive that searching the forums does work: View Thread *
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *
    * Use Offensive Programming, not Defensive Programming. * On Error Resume Next is error ignoring, not error handling(tm).
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN

Posting Permissions

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