Sorting Numbers in a text file (vb6)

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
jwnrbAsked:
Who is Participating?
 
David LeeConnect With a Mentor Commented:
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
 
David LeeCommented:
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
 
jwnrbAuthor Commented:
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
jwnrbAuthor Commented:
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
 
David LeeCommented:
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
 
David LeeCommented:
date should be data in my last post.  
0
 
jwnrbAuthor Commented:
hmm, okay, so how do i throw sorted and reverse sorted into a file?
0
 
jwnrbAuthor Commented:
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
 
vinnyd79Commented:
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
 
David LeeCommented:
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
 
jwnrbAuthor Commented:
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
 
jwnrbAuthor Commented:
Okay its enough for me now, i can finish the rest later on.
thanks for all your help

j
0
 
David LeeCommented:
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
 
jwnrbAuthor Commented:
thanks so much, its nearly complerte and all is well.  thanks and regards,
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.