+ Reply to Thread
Results 1 to 8 of 8

Sorting an Array

  1. #1
    Registered User
    Join Date
    04-15-2013
    Location
    new york city
    MS-Off Ver
    Excel 2010
    Posts
    24

    Sorting an Array

    Hi all -

    I am learning VBA and need some help understanding how the following code is able to organize an array of strings in alphabetical order. With regard to the For...Next section of code, I'd like to understand how checking if strArray(X) > strArray (Y) results in an alphabetized list. I'm also confused by the assignments - what does it mean when strTemp = strArray(X) and yet in the very next line of code strArray(X) = strArray(Y), and so on. If someone could explain in words how this section alphabetizes the array I'd really appreciate it. Thanks!
    ________________________________________________________________________________________________________________________

    Please Login or Register  to view this content.
    Last edited by vba_beginner; 04-23-2013 at 10:33 PM. Reason: code text

  2. #2
    Registered User
    Join Date
    04-15-2013
    Location
    new york city
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Sorting an Array

    To further explain my question, if strArray(X) and strArray(Y) refer to strings, such as 'nihilism' and 'defeatism', then how does the If...Then statement:

    Please Login or Register  to view this content.
    work if the values associated with strArray(X) and strArray(Y) aren't numeric values, but rather strings of text? How is it that this formula can determine the word 'defeatism' precedes 'nihilism' using a mathematical operator like ">"? Thanks!

  3. #3
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,945

    Re: Sorting an Array

    THis is a VBA question, so I will move it there where it belongs
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  4. #4
    Registered User
    Join Date
    04-15-2013
    Location
    new york city
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Sorting an Array

    Sorry, I thought I posted this thread under 'Excel Programming / VBA / Macros'. If that's not the correct forum, then can you kindly tell me the correct forum? Thank you!

  5. #5
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,844

    Re: Sorting an Array

    work if the values associated with strArray(X) and strArray(Y) aren't numeric values, but rather strings of text? How is it that this formula can determine the word 'defeatism' precedes 'nihilism' using a mathematical operator like ">"? Thanks!
    When dealing with text strings, maybe don't think of "less than" or "greater than" but rather "comes before" or "comes after" alphabetically. so a<b<bc<d<def<eag<zzz I think somewhere in the help files is an official list of VBA/ASCII/ANSI official sort order.

    I'm not very familiar with sorting algorithms, and it might be more than you want to know anyway. I think the specific algorithm you have there is called a bubble sort http://en.wikipedia.org/wiki/Bubble_sort If you are interested, this and other pages may help you better understand how this particular algorithm works. Depending on where you are at in learning programming, it also might introduce you to other sorting algorithms.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  6. #6
    Registered User
    Join Date
    04-15-2013
    Location
    new york city
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Sorting an Array

    Ah, okay, I think that answers my question as I do remember seeing a paragraph about 'bubble sort', but didn't pay enough attention to it. It states:

    "the bubble sort, so called because the items being sorted to the earlier positions in the array gradually bubble up to the top. The bubble sort consists of two loops that compare two items in the array; if the second item belongs further up the list than the first item, the sort reverses their positions, and the comparisons continue until the whole list is sorted into order." - Mastering VBA for Microsoft Office 2010 by Richard Mansfield.

    Many thanks MrShorty.

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

    Re: Sorting an Array

    Hey vba,

    The stuff on the left side of the equation in VBA is REPLACED by the stuff on the right. It doesn't mean they are equal.
    So when the code says:
    strTemp = strArray(X)
    it means the strTemp is overwritten with whatever strArray(X) is.

    You need to keep a temp copy of this X guy because in the next line you overwrite it with the line of:
    strArray(X) = strArray(Y)
    After that line of code is run, both strArray(X) and strArray(Y) are the same thing. Remember you overwrite the stuff on the left side of the equal with the stuff on the right side of the equal.

    The code has stored a tempory value of strArray(X) and then in the next line shoves that value/string into the strArray(Y) spot.

    I hope that helps.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  8. #8
    Registered User
    Join Date
    04-15-2013
    Location
    new york city
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Sorting an Array

    Very helpful, thank you for the clarification.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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