CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2011
    Posts
    14

    Sorting an ASCII File

    Hello:
    Many may have seen the struggle I am having with sorting a TREEVIEW. After many, many hours I am trying to think of another way of accomplishing this.

    I thought perhaps I could write the TV to an ASCII file, then sort that file and then relabel it, followed by a re-reading into a TV.

    So, if I can get any help it would be greatly appreciated.

    I am able to export the file to an ASCII TAB Delimited file as follows:

    Code:
    A    75023
    	AA        75043
    		AAA       75045
    			AAA1      95045
    			AAA2      15045
    			AAA3      35045
    			AAA4      55045
    		AAB       75047
    			AAB1      95047*
    			AAB2      15047
    			AAB3      35047
    			AAB4      55047
    	AB        75063*
    		ABA       75065
    			ABA1      95065
    			ABA2      15065
    			ABA3      35065
    			ABA4      55065
    		ABB       75067
    			ABB1      95067
    			ABB2      15067
    			ABB3      35067*
    			ABB4      55067
    	AC        75083*
    		ACA       75085*
    			ACA1      95085
    			ACA2      15085
    			ACA3      35085
    			ACA4      55085
    		ACB       75087
    			ACB1      95087
    			ACB2      15087
    			ACB3      35087
    	AD        75003
    		ADA       75005
    			ADA1      95005
    			ADA2      15005
    			ADA3      35005
    			ADA4      55005
    		ADB       75007
    			ADB1      95007
    			ADB2      15007
    			ADB3      35007
    So now what I need to do is sort anything that ends with an "*" to the bottom of its section/node.
    I have to keep the integrity of the the nodes and tab delimited file, so I can read it back in.

    Start at the highest level.. And sort everything ending with an "*" at that level to the bottom. (Within that node)

    So (Level2) AB and AC will go to the end. AND everything under it.

    Next go to the next level (Level3) ACA will go to the end of it's level.

    Next go to the next level (level4). AAB1 will go after AAB4 and ABB3 will go after ABB4.

    The output would look something like this:
    Code:
    A    75023
    	AA        75043
    		AAA       75045
    			AAA1      95045
    			AAA2      15045
    			AAA3      35045
    			AAA4      55045
    		AAB       75047
    			AAB2      15047
    			AAB3      35047
    			AAB4      55047
    			AAB1      95047*
    	AD        75003
    		ADA       75005
    			ADA1      95005
    			ADA2      15005
    			ADA3      35005
    			ADA4      55005
    		ADB       75007
    			ADB1      95007
    			ADB2      15007
    			ADB3      35007
    	AB        75063*
    		ABA       75065
    			ABA1      95065
    			ABA2      15065
    			ABA3      35065
    			ABA4      55065
    		ABB       75067
    			ABB1      95067
    			ABB2      15067
    			ABB4      55067
    			ABB3      35067*
    	AC        75083*
    		ACB       75087
    			ACB1      95087
    			ACB2      15087
    			ACB3      35087
    		ACA       75085*
    			ACA1      95085
    			ACA2      15085
    			ACA3      35085
    			ACA4      55085
    If someone can help me with this, it would be greatly appreciated.

    The next step would be to then go back through the newly sorted file and rename the left side of the values. AA, AAA, AAA1, etc. back to the original order. So, if you can provide that piece of code as well, it would be greatly appreciated. But I think I can figure that part out, if I can just get some help with the sorting of the file.

    Thanks in advance.
    k

  2. #2
    Join Date
    Jun 2011
    Posts
    14

    Re: Sorting an ASCII File

    Hello:
    I have worked through an example, hoping this makes what is needed a little clearer.

    Thank you in advance for any and all support.

    Thanks.
    I need a small piece of code that will read an ASCII Delimited file and sort it, with the following rules.
    This might seem complicated, but I think a good tight loop or two should accomplished what is needed.

    NOTE: the tabs and delimiters need to remain so it can be re-read into a TREEVIEW.

    INPUT: Tab delimited ASCII File.
    OUTPUT: Tab delimited ASCII File with values ending with an "*" are sorted to the end of their node.

    -This file should be thought of as a TREE Structure
    -The left side of the labels. DO NOT MOVE
    -The right side of the labels DO MOVE
    -The left and right are determined by the first space on the right side
    So using this as an example: AA 8975043*
    AA+SPACE(8) is the left label
    8975043* is the right label
    AA+SPACE(8) will not move
    8975043* - since it ends with an "*" will be moved to the end of the NODE

    1. All right labels ending with an "*" need to be moved/sorted to the end of their node
    2. Start with the second level and sort the items ending with an "*" to the end of that node
    So:
    AA 8975043*
    AB 175063*
    AC 75083
    AD 75003
    Becomes:
    AA 75083
    AB 75003
    AC 8975043*
    AD 175063*
    NOTE: the AA, AB, AC, AD labels DO NOT MOVE

    3. The values under each node ARE MOVED AS WELL.
    So:
    Code:
        	AA      8975043*
    		AAA       75045
    			AAA1      95045
    			AAA2      15045
    			AAA3      35045
    			AAA4      55045
    		AAB      175047
    			AAB1     395047
    			AAB2      15047
    			AAB3    1235047*
    			AAB4      55047
    		AAC       75049
    			AAC1      95049
    			AAC2      15049
    			AAC3      35049
    			AAC4      55049
    		AAD     2475041
    			AAD1      95041
    			AAD2      15041
    			AAD3      35041
    			AAD4      55041
    	AB       175063*
    	AC        75083
    	AD        75003
    Becomes:
    Code:
    	AA        75083
    	AB        75003
        	AC      8975043*
    		ACA       75045
    			ACA1      95045
    			ACA2      15045
    			ACA3      35045
    			ACA4      55045
    		ACB      175047
    			ACB1     395047
    			ACB2      15047
    			ACB3    1235047*
    			ACB4      55047
    		ACC       75049*
    			ACC1      95049
    			ACC2      15049
    			ACC3      35049
    			ACC4      55049
    		ACD     2475041
    			ACD1      95041
    			ACD2      15041
    			ACD3      35041
    			ACD4      55041
    	AD       175063*
    4. Next we need to move to Level 3 and do the same as we did with Level 2
    So:
    Code:
        	AC      8975043*
    		ACA       75045
    			ACA1      95045
    			ACA2      15045
    			ACA3      35045
    			ACA4      55045
    		ACB      175047
    			ACB1     395047
    			ACB2      15047
    			ACB3    1235047*
    			ACB4      55047
    		ACC       75049*
    			ACC1      95049
    			ACC2      15049
    			ACC3      35049
    			ACC4      55049
    		ACD     2475041
    			ACD1      95041
    			ACD2      15041
    			ACD3      35041
    			ACD4      55041
    Becomes:
    Code:
        	AC      8975043*
    		ACA       75045
    			ACA1      95045
    			ACA2      15045
    			ACA3      35045
    			ACA4      55045
    		ACB      175047
    			ACB1     395047
    			ACB2      15047
    			ACB3    1235047*
    			ACB4      55047
    		ACC     2475041
    			ACC1      95041
    			ACC2      15041
    			ACC3      35041
    			ACC4      55041
    		ACD       75049*
    			ACD1      95049
    			ACD2      15049
    			ACD3      35049
    			ACD4      55049
    5, Next move to Level 4, Level 5, Level 6, etc. there can be up to 9 Levels

    BEFORE SORT/MOVE
    Code:
    A    75023
    	AA      8975043*
    		AAA       75045
    			AAA1      95045
    			AAA2      15045
    			AAA3      35045
    			AAA4      55045
    		AAB      175047
    			AAB1     395047
    			AAB2      15047
    			AAB4    1235047*
    			AAB4      55047
    		AAC       75049*
    			AAC1      95049
    			AAC2      15049
    			AAC3      35049
    			AAC4      55049
    		AAD     2475041
    			AAD1      95041
    			AAD2      15041
    			AAD3      35041
    			AAD4      55041
    	AB       175063*
    		ABA       75065
    			ABA1      95065
    			ABA2      15065
    			ABA3      35065
    			ABA4      55065
    		ABB       75067*
    			ABB1      95067
    			ABB2      15067
    			ABB3      35067
    			ABB4      55067
    		ABC       75069
    			ABC1   43295069*
    			ABC2      15069
    			ABC3      35069
    			ABC4      55069
    		ABD       75061
    			ABD1      95061
    			ABD2     515061*
    			ABD3      35061
    			ABD4      55061
    	AC        75083
    		ACA       75085*
    			ACA1      95085*
    			ACA2      15085*
    			ACA3      35085*
    			ACA4      55085*
    		ACB       75087*
    			ACB1      95087*
    			ACB2      15087*
    			ACB3      35087*
    			ACB4      55087*
    		ACC       75089*
    			ACC1      95089*
    			ACC2      15089*
    			ACC3      35089*
    			ACC4      55089*
    		ACD      675081
    			ACD1      95081*
    			ACD2      15081*
    			ACD3      35081*
    			ACD4      55081*
    	AD        75003
    		ADA       75005
    			ADA1      95005
    			ADA2      15005
    			ADA3      35005
    			ADA4      55005
    		ADB       75007
    			ADB1      95007
    			ADB2      15007
    			ADB3      35007
    			ADB4      55007
    		ADC       75009*
    			ADC1      95009*
    			ADC2      15009*
    			ADC3      35009*
    			ADC4      55009*
    		ADD       75001
    			ADD1      95001
    			ADD2      15001
    			ADD3      35001
    			ADD4      55001
    AFTER SORT/MOVE
    Code:
     A    75023
    	AA        75083
    		AAA      675081
    			AAA1      95081*
    			AAA2      15081*
    			AAA3      35081*
    			AAA4      55081*
    		AAB       75085*
    			AAB1      95085*
    			AAB2      15085*
    			AAB3      35085*
    			AAB4      55085*
    		AAC       75087*
    			AAC1      95087*
    			AAC2      15087*
    			AAC3      35087*
    			AAC4      55087*
    		AAD       75089*
    			AAD1      95089*
    			AAD2      15089*
    			AAD3      35089*
    			AAD4      55089*
    	AB        75003
    		ABA       75005
    			ABA1      95005
    			ABA2      15005
    			ABA3      35005
    			ABA4      55005
    		ABB       75007
    			ABB1      95007
    			ABB2      15007
    			ABB3      35007
    			ABB4      55007
    		ABC       75009*
    			ABC1      95009*
    			ABC2      15009*
    			ABC3      35009*
    			ABC4      55009*
    		ABC       75001
    			ABC1      95001
    			ABC2      15001
    			ABC3      35001
    			ABC4      55001
    		ABD       75009*
    			ABD1      95009*
    			ABD2      15009*
    			ABD3      35009*
    			ABD4      55009*
    
    	AC      8975043*
    		ACA       75045
    			ACA1      95045
    			ACA2      15045
    			ACA3      35045
    			ACA4      55045
    		ACB      175047
    			ACB1     395047
    			ACB2      15047
    			ACB3      55047
    			ACB4    1235047*
    		ACC     2475041
    			ACC1      95041
    			ACC2      15041
    			ACC3      35041
    			ACC4      55041
    		ACD       75049*
    			ACD1      95049
    			ACD2      15049
    			ACD3      35049
    			ACD4      55049
    	AD       175063*
    		ADA       75065
    			ADA1      95065
    			ADA2      15065
    			ADA3      35065
    			ADA4      55065
    		ADB       75069
    			ADB1      15069
    			ADB2      35069
    			ADB3      55069
    			ADB4   43295069*
    		ADc       75061
    			ADC1      95061
    			ADC2      35061
    			ADC3      55061
    			ADC4     515061*
    		ADD       75067*
    			ADD1      95067
    			ADD2      15067
    			ADD3      35067
    			ADD4      55067

  3. #3
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,900

    Re: Sorting an ASCII File

    First - You want to retain Tabs in your ASCII data - Substitute a special Character for each as you extract your data to be sorted

    eg, AA123456 <TAB> 1234 Could be extracted as AA123456:1234

    You want to actually change the prefix of the data if it has an * - then change it as you create the data to be sorted (NOT WHILE YOU ARE SORTING)

    To sort data of the quantity you have you have 2 alternatives

    1) Update a Table in Access Database then using SQL use a Select statement ASC (Ascending) to sort it (Select * from myTable Order By MyData ASC)

    2) Use a List Component on a Form which has been set up to be Sorted and add your data to it (Make it Not Visible). Then read the data back 1 row at a time and it will come back sorted

    Option 2 is the easiest as you don't need a database

  4. #4
    Join Date
    Feb 2013
    Location
    United States
    Posts
    56

    Re: Sorting an ASCII File

    I would think this all could be easily accomplished with an internal data structure that you create, without creating any external files or databases. Then again, I am a C++ programmer. When I used to program in VB, I would create a user defined type to represent the nodes in a tree data structure. Then I would create an array of these nodes and use the array indexes as pointers. (I love pointers. )

    Maybe you could put the data into a two dimensional array with a column for each level. Then sort the table as if the all of the columns are one big column. Then relabel the left side of the labels so that they are in order. When you extract the data from the sorted table, check if the left side of each label is different than the previous row. This will indicate that the previous row was a leaf in the tree.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured