Solved

Sorting Numbers in a text file (vb6)

Posted on 2004-05-02
14
2,936 Views
Last Modified: 2012-06-21
Hi, I have a file in which data gets populated, line by line with double floating point.  
if i push a cmdButton, it should sort the numbers, lowest to highest, and another one, highest to lowest.  is there a built in function, or would a bubble sort suffice?

thanks
0
Comment
Question by:jwnrb
  • 7
  • 6
14 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 10974056
One really quick way to sort something is to use a listbox with the sorted property set to true.  With numbers though you'll need to make sure they're all the same length on the integer side.  Otherwise you'll get a sort like 1, 10, 100 instead of 1, 2, 3 etc.
0
 

Author Comment

by:jwnrb
ID: 10974090
actually i am using a listbox to display stuff (but i am doing some manipulations); but i need the data sorted or desorted in the file it self.

0
 
LVL 76

Accepted Solution

by:
David Lee earned 50 total points
ID: 10974148
I understand that.  I'm not aware of any way to sort a file without reading in its contents, performing the sort, and writing the sorted material back out into the file.  You can certainly use a bubble sort if you want but it'd save a lot of coding to read the file into a listbox with the sorted property turned on, let the listbox do the sorting for you, and write the results back out to the file.  The listbox in question can remain hidden from view the entire time.
0
 

Author Comment

by:jwnrb
ID: 10974171
well i can sort the data 1 - 100 using the list box, but what about reverse?  i'd need to have 100-0 as well.

i suppose i can open the file and store the data into an array and sort it


0
 
LVL 76

Expert Comment

by:David Lee
ID: 10974186
Just read the listbox in reverse order to get the date in decending sequence.

    For x = (List1.ListCount - 1) To 0 Step -1
        do whatever with List1.List(x)
    Next
0
 
LVL 76

Expert Comment

by:David Lee
ID: 10974199
date should be data in my last post.  
0
 

Author Comment

by:jwnrb
ID: 10974212
hmm, okay, so how do i throw sorted and reverse sorted into a file?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:jwnrb
ID: 10974224
pretty much i have this:

Private Sub cmdDescend_Click()
    lstDisplay.Clear
       ' open the file of items and toss them into an array
   Dim spherevol(15) As Integer
   Open App.Path & "/myVolume.txt" For Input As #1
   Do Until EOF(1)
      Line Input #1, Data
      spherevol(i) = Data
      lstDisplay.AddItem (Data)
   Loop
   
   For x = (lstDisplay.ListCount - 1) To 0 Step -1
        do whatever with List1.List(x)
    Next
     
   Close #1
   
 
   
    ' toss them back into a new file
   Open App.Path & "/myVolume.txt" For Output As #1
   
   For i = 1 To 15
      Write #1, spherevol(i)
   Next i
   Close #1
End Sub

and

Private Sub cmdAscend_Click()
   ' open the file of items and toss them into an array
   lstDisplay.Clear
   Dim spherevol(15) As Integer
   Open App.Path & "/myVolume.txt" For Input As #1
   Do Until EOF(1)
      Line Input #1, Data
      spherevol(i) = Data
      lstDisplay.AddItem (Data)
   Loop
   
   lstDisplay.Sorted
       
   Close #1
   
 
 
   
    ' toss them back into a new file
   Open App.Path & "/myVolume.txt" For Output As #1
   
   For i = 1 To 15
      Write #1, spherevol(i)
   Next i
   Close #1
 End Sub
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 10976517
You could shell the command interpretor and use sort to build a new sorted file.

Private Sub Command1_Click()
Shell Environ("ComSpec") & " /c sort < C:\Somefile.txt > C:\NewFile.txt", vbNormalFocus
End Sub
0
 
LVL 76

Expert Comment

by:David Lee
ID: 10976960
Vinny is absolutely right.  I'd forgotten about the Sort command.  If the data is already in a file and all you want to do is sort it, then the Sort approach is the simplest.  But the Sort command suffers from the same problem I mentioned above.  If the numbers aren't the same length on the left side of the decimal point, then the sort isn't going to produce the desired results.  For example, assume we have a file containing the following numbers:

2.54
1.45
10.99
3.60

If we Sort the file or use the listbox approach without padding the integer portion of the numbers with leading zeros, then the resulting sort will produce this output:

1.45
10.99
2.54
3.60

At least that's what I see happening.  Maybe Vinny knows of a way to get around this.  I solved it by writing a short function that pads a number to a give length.  Here's that function.

Private Function Pad(dblValue As Double, intLength As Integer) As String
    Dim intValue As Integer
    intValue = Len(Trim(Str(Int(dblValue))))
    Pad = String(intLength - intValue, "0") & Trim(Str(dblValue))
End Function

Let us know which way you want to proceed.
0
 

Author Comment

by:jwnrb
ID: 10978918
oh that does make sense. similarly i see that in pre xp on files in explorer.

ii think the list box idea seems the best method. it sorts it on screen and saves a copy to disk.  
which i can see working, however, how do i reverse sort it on display on listbox and then save that as well?
0
 

Author Comment

by:jwnrb
ID: 10979260
Okay its enough for me now, i can finish the rest later on.
thanks for all your help

j
0
 
LVL 76

Expert Comment

by:David Lee
ID: 10979366
You can't reverse sort it in the display unless I'm missing something.  You can turn sorting on or off but I don't know of any means of cotrolling the direction fo the sort.  If you just need to sort the data in descending sequence and write it out to a file then the code I posted above

    For x = (List1.ListCount - 1) To 0 Step -1
        do whatever with List1.List(x)
    Next

will do just that.  All you need to do is replace the statement inside the For Next loop with the command to write to a file.  If you have to display the list in descedning sequence, then you have a couple of choices.  One, you can copy the contents of the list into an array using the code above to copy it in reverse order, clear the list, then copy from the array back to the list.  Two, you could put a second listbox on the form laying at the same coordinates as the other list.  Copy the sorted list, again using the code above, from one list to the other list, then disable one list and enable the other.  Assume we have two lists, lbAscending and lbDescending.  lbAscedning has its Sorted property set to True, lbDescending has its Sorted property set to False.  If the user wants to see the ascending list, then we set lbDescedning.Visible to False and lbAscending.Visible to True.  We reverse the process to see the list in descending sequence.  If we've loaded all the values into lbAscending and need to copy them to lbDescending, then we do it like this:

    For x = (lbAscending.ListCount - 1) To 0 Step -1
        lbDescending.AddItem lbAscending.List(x)
    Next
0
 

Author Comment

by:jwnrb
ID: 10979950
thanks so much, its nearly complerte and all is well.  thanks and regards,
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Saving history changes to sub form 4 26
Excel - Save a copy of work book 13 80
Copy a row 12 53
How to Add / Edit Windows Menu 4 26
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

759 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

22 Experts available now in Live!

Get 1:1 Help Now