Solved

# Sorting Numbers in a text file (vb6)

Posted on 2004-05-02
2,946 Views
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
Question by:jwnrb
• 7
• 6

LVL 76

Expert Comment

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

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

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

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

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

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

Author Comment

ID: 10974212
hmm, okay, so how do i throw sorted and reverse sorted into a file?
0

Author Comment

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
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
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

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

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

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

ID: 10979260
Okay its enough for me now, i can finish the rest later on.

j
0

LVL 76

Expert Comment

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
Next
0

Author Comment

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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we aâ€¦
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describesâ€¦
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). Uâ€¦
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process fromâ€¦