Sorting ArrayList of Integers

Posted on 2007-07-24
Last Modified: 2013-11-07
Hi there,

I have an ArrayList (al) containg integers. When I use al.Sort() i want items to be properly ordered.

Instead of results like this: 1, 2, 3, 10, 12, 16, 23 (would be good)
I get results like this: 1, 10, 12, 16, 2, 23, 3 (is bad)

How do I do it?



Question by:11login
    LVL 9

    Assisted Solution

    Can you post some code?  ArrayList.Sort should work as expected.
    LVL 27

    Accepted Solution

    Here's an example of how you can do it:

        Private Sub SortItems()

            Dim list As New System.Collections.Generic.List(Of System.Int32)

            'Add your items here
            list.AddRange(New Int32() {1, 10, 12, 16, 2, 23, 3})

            list.Sort(AddressOf SortList)

            For Each i As System.Int32 In list

        End Sub

        Private Function SortList(ByVal x As System.Int32, _
            ByVal y As System.Int32) As Integer

            Return x.CompareTo(y)

        End Function

        Private Sub PrintResults(ByVal i As System.Int32)

        End Sub

    LVL 96

    Assisted Solution

    by:Bob Learned
    It looks like you have strings, and not integers in the array.


    Author Comment

    Yikes, that was embarrasing... this was the code:
    Dim alCount As New ArrayList
    For Each iValue As String hCount.Values()

    I actually added the values as strings, although there where integers in the hashtable. I made the change and it works now (of course). Thanks a lot, all of you!

    LVL 9

    Expert Comment

    I suppose this is actually a good example of why using generics is a good thing - the compiler would have spotted this mistake.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now