Consulting

Results 1 to 4 of 4

Thread: Sorting an array of delimited strings

  1. #1
    VBAX Regular
    Joined
    Jul 2014
    Posts
    79
    Location

    Sorting an array of delimited strings

    Guys,

    I am looking for some advice regarding the following problem. I think I might be over complicating it.

    I have an array where each element is a delimited string. Each delimited string holds data associated with a Content Control in the ActiveDocument, e.g.

    arrCCData(1) = 2|21|CC2 in textbox 1|16
    arrCCData(2) = 1|56|Current Project Cost in Pounds|30
    arrCCData(3) = 1|113|Completion Date|15
    arrCCData(4) = 1|32|Contractor|10
    arrCCData(5) = 1|371|Video|5
    arrCCData(6) = 2|1|CC1 in textbox 1|16
    arrCCData(7) = 0|1|ClientSurveyNumber|18
    arrCCData(8) = 1|399|way|3
    arrCCData(9) = 1|527|You can also|12
    arrCCData(10) = 1|5|Client|6
    arrCCData(11) = 0|1|Footer CC|9
    arrCCData(12) = 0|22|SurveyDate|10


    I am trying to sort the array by token 1 of each element to achieve:

    0|1|ClientSurveyNumber|18
    0|1|Footer CC|9
    0|22|SurveyDate|10
    1|5|Client|6
    1|32|Contractor|10
    1|56|Current Project Cost in Pounds|30
    1|113|Completion Date|15
    1|371|Video|5
    1|399|way|3
    1|527|You can also|12
    2|1|CC1 in textbox 1|16
    2|21|CC2 in textbox 1|16


    Then finally by token 2 to achieve the final result:

    0|1|ClientSurveyNumber|18
    0|1|Footer CC|9
    0|22|SurveyDate|10
    1|5|Client|6
    1|32|Contractor|10
    1|56|Current Project Cost in Pounds|30
    1|113|Completion Date|15
    1|371|Video|5
    1|399|way|3
    1|527|You can also|12
    2|1|CC1 in textbox 1|16
    2|21|CC2 in textbox 1|16

    What is the easiest way to do this?

    I am currently using a customized quicksort procedure to sort the array by token 2 of each element. I am then looping through the partially sorted array and copying the elements into a separate array. The position in the new array is based on token 1.

    It's easier to explain by example. Any advice on the best way to do this would be appreciated. See attached.

    Joe
    Attached Files Attached Files

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Hi Joe,

    I would probably do this with a userform (never shown) and a ListBox sort function.

    See attached file.
    Attached Files Attached Files
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Joe,

    Modify that code as follows. I had taken the line out and now realizes it needs to be there:

    With oFRm
        .ListBox1.List = arrCCData
        .ListBox1.ColumnCount = 4 'Add this line.
    Greg

    Visit my website: http://gregmaxey.com

  4. #4
    VBAX Regular
    Joined
    Jul 2014
    Posts
    79
    Location
    Greg, thanks. I will have a look at this tomorrow but I hadn't even thought of using a listbox control to solve that problem. I suppose it may be better using built-in functionality where possible. I have no idea what approach will be faster for large amounts of data - maybe that is a test I could do.

    Joe

Posting Permissions

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