+ Reply to Thread
Results 1 to 23 of 23

Sorting an array of strings into alphabetical order

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Sorting an array of strings into alphabetical order

    Hi all,

    Usually i just use the excel worksheet to sort my values and then read the sorted column of strings into an array.

    However, i have an array of strings around 30000 strings......already in array.......and might get larger. To paste into excel, use excel to sort and then read back into arrays.......i thought might get time consuming so i was wondering how to sort an array of strings into alphabetical order.

    I read something in John Walkenbach's book on sorting and he indicated the "VBA COunting sort" was the best.....could i use this? or which ever way you all see as best is fine too.

  2. #2
    Forum Contributor
    Join Date
    01-14-2014
    Location
    London, England
    MS-Off Ver
    Excel 2013
    Posts
    240

    Re: Sorting an array of strings into alphabetical order

    http://www.cpearson.com/excel/SortingArrays.aspx

    In my experience excel will be far quicker than code alone given the size of the array you want to sort.
    Please consider adding a * if I helped.

  3. #3
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Sorting an array of strings into alphabetical order

    You might find this helpful.
    Please Login or Register  to view this content.
    If you don't want to visit the link to get my speed routines, in a Module:
    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: Sorting an array of strings into alphabetical order

    what link were you referring to?

  5. #5
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: Sorting an array of strings into alphabetical order

    ok i see the links you were referring to.........

  6. #6
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Sorting an array of strings into alphabetical order

    This would be a little faster:
    Please Login or Register  to view this content.

  7. #7
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,330

    Re: Sorting an array of strings into alphabetical order

    Hi welchs,

    There are lots of sorting algorithms that have been developed. I believe putting the data into Excel and letting it sort (using C code) is much faster than trying to do it yourself in VBA. See some algorithms at:
    http://www.vbadeveloper.net/sortingv...rtionquick.pdf
    https://social.msdn.microsoft.com/Fo...n?forum=isvvba
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  8. #8
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Sorting an array of strings into alphabetical order

    @MarvinP I suspect that it depends on how much data you are trying to sort, you have 2 worksheet calls doing it your way and they are very expensive

    I reckon that quicksort would be faster than using an arraylist though

  9. #9
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Sorting an array of strings into alphabetical order

    Quick Test:
    Please Login or Register  to view this content.
    So as a quick rule of thumb, one needs to be sorting a huge amount of data before sorting on the worksheet becomes the quickest - my code for reference incase I've made a mess

    Main
    Please Login or Register  to view this content.
    Sorting functions
    Please Login or Register  to view this content.
    With the timer from: http://stackoverflow.com/questions/1...me-of-vba-code

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Sorting an array of strings into alphabetical order

    Kyle, a few questions if I may:

    1.
    I reckon that quicksort would be faster than using an arraylist though
    Why?

    (EDIT: Thanks for the timings; that's interesting and surprising.)

    It appears that the Sort method is not stable (like QuickSort). Sorting "The quick brown fox jumped over the lazy dog's back" returns as its last two items "the" and "The"

    2. How do you set case sensitivity for the sort?
    Last edited by shg; 02-03-2015 at 01:48 PM.
    Entia non sunt multiplicanda sine necessitate

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Sorting an array of strings into alphabetical order

    And ...

    I believe all of those System.Collections thingies are in mscoree.dll. What reference would you set for early binding?

  12. #12
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Sorting an array of strings into alphabetical order

    I believe all of those System.Collections thingies are in mscoree.dll. What reference would you set for early binding? - never found it if it exists! Wouldn't be much help anyway since a lot of the methods are overloaded - so you end up having to fix numbers to the methods to get it to call the one you want - can't remember exactly which ones but I recall having to do it at some point.

    I think it's slower since it's a collection and re-indexes each time you add an element - should be easy to test; so it's really the populating rather than the sorting. Once it's sorted though it can binary search so it can be useful if you need to search for multiple elements

    As to setting case sensitivity on the sort, I'm not sure you can in VBA, you pass a comparer in .Net but I doubt this is possible in VBA

  13. #13
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Sorting an array of strings into alphabetical order

    Interesting, thank you.

  14. #14
    Forum Contributor
    Join Date
    01-14-2014
    Location
    London, England
    MS-Off Ver
    Excel 2013
    Posts
    240

    Re: Sorting an array of strings into alphabetical order

    @Kyle123 I'd be interested if you could repeat your analysis but turn off screenupdating / calculation etc - all the things you'd normally do to speed up vba in the worksheet sort.

  15. #15
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Sorting an array of strings into alphabetical order

    They are turned off for the spreadsheet sort using Kenneth's code


    Sent from my iPhone using Tapatalk

  16. #16
    Forum Contributor
    Join Date
    01-14-2014
    Location
    London, England
    MS-Off Ver
    Excel 2013
    Posts
    240

    Re: Sorting an array of strings into alphabetical order

    ahh spotted ta - your worksheet sort seems to be extremely slow.... What about using a variant data type with a second dimension so you can do away with the transpose that you have to run twice?

    I normally find 15000 records (typically people name data) is my tipping point to worksheet sort from straight VBA, but i guess depends on what your data looks like.

    I guess trial and error is the answer!

  17. #17
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Sorting an array of strings into alphabetical order

    The right solution depends on the context and speed isn't always the be all and end all. Each of these methods has pros and cons so it depends what you want to do.

    Re the 2d array, that isn't really the same problem so it's comparing apples and oranges. Sorting on a worksheet offers simplicity - especially if the data is in 2 dimensions. The transpose is definitely slow but necessary for comparison, I've still got this set up on my machine at work so I'll run it for you tomorrow if you like using a 2d array which will make things faster - but you do still have worksheet calls which are slow


    Sent from my iPhone using Tapatalk

  18. #18
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Sorting an array of strings into alphabetical order

    If you're sorting 2D data, it's probably important that you use a stable sort.

  19. #19
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: Sorting an array of strings into alphabetical order

    i am thinking my file might get as large as 60000 rows.........is the excel sort still the fastest. i know its the easiest in terms of understanding stuff.

  20. #20
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Sorting an array of strings into alphabetical order

    No, quick sort is more than twice as fast


    Sent from my iPhone using Tapatalk

  21. #21
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Sorting an array of strings into alphabetical order

    Upon reading a little further, I was wrong on the internal structure of the arraylist. It is actually comprised of an internal array which essentially ReDim Preserves when you add elements* - this explains why it is slow adding elements.

    Further reading available below:

    *The redim preserve is more efficient than the traditional method:
    Quote Originally Posted by http://blogs.msdn.com/b/bclteam/archive/2004/12/01/273454.aspx
    When the internal array is not large enough to hold all items (happens when new items are added to an ArrayList object,) a new array with double size of the original array will be created. All the items in the ArrayList will be copied from the old Array to the new Array. The new Array will be used from here on and the old array will be reclaimed by garbage collector sooner or later.
    EDIT:
    Having read the article about the inner workings, one would assume that specifying the capacity of the ArrayList in advance of population would increase the performance:
    Please Login or Register  to view this content.
    This doesn't unfortunately doesn't appear to have any real impact on performance. FWIW the actual sort and return of array is faster than quicksort as you'd expect - the issue is still the population
    Last edited by Kyle123; 02-04-2015 at 05:45 AM.

  22. #22
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,985

    Re: Sorting an array of strings into alphabetical order

    FWIW, you can set a reference to mscorlib.dll and then just declare as ArrayList.
    Everyone who confuses correlation and causation ends up dead.

  23. #23
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Sorting an array of strings into alphabetical order

    Welchs101, hopefully you have your solution now. There has been several good discussions in this thread. You asked for sorting an array, not a worksheet range, so you have several methods to pick from now. I tried a VBA Counting Sort routine but it did not work for a string array. Ultimately, my guess is that you would want to write the sorted data to an Excel range or an external data file which would change the speed numbers.

    Romperstomper, I could not set a reference to the mscorlib.dll file to see if early binding increased speed for that method. I tried regsvr32 to register it but it did not work. Maybe a regasm would set the reference listing.

    Using several sorting methods in the attached file, I found this below. I included an ADO method but did not take time to finish it for testing. ADO methods tend to be a bit slow. Note that the jscript method was the 2nd fastest while the quick sort was the fastest. Real world data should be used for testing speeds though. When there are duplicates, that can cause problems for some routines. In the jscript method, if there is a value of error for an element, it will fail. From the data below, for really large sort tests, be sure to remove the really slow ones first.

    Lastly, keep in mind that speed is not everything. Stability is a more critical.

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by Kenneth Hobson; 02-05-2015 at 01:31 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Sorting a table into alphabetical order?
    By elliottt in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 02-21-2014, 12:57 PM
  2. Automatically sorting in to alphabetical order
    By macky18 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 01-15-2013, 07:45 AM
  3. Sorting the alphabetical order with semicolon in each cell.
    By spiderkzn in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-07-2012, 09:55 AM
  4. Sorting Colums into alphabetical order
    By tox in forum Excel General
    Replies: 3
    Last Post: 02-22-2005, 12:06 PM
  5. Sorting rows into alphabetical order?
    By tox in forum Excel General
    Replies: 8
    Last Post: 02-04-2005, 07:08 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1