alphabetical sort problem

dood
01-14-2008, 06:33 PM
I am to program code that sorts multiple names in arrays into alphabetical order. I have the code written and I have checked over it many times, but it is not working.

Function ascend(inName As String, position As Integer) As Integer
ascend = Asc(Mid(inName, position, 1))
End Function

This just gets the character from a string in the position stated in the parameter.

Private Sub cmdImport_Click()
filename = InputBox("What is the name of the file you wish to import?") 'ask for filename

filename = "/" + filename 'add / to filename

Open App.Path + filename For Input As #1 'open file to use
Do While EOF(1) = False
i = i + 1 'add 1 to counter
Input #1, nam(i), phNum(i) 'get name and number

lblName(i).Caption = nam(i) 'display name, number and entry number
lblNumber(i).Caption = phNum(i)
lblEntry(i).Caption = i
Loop
Close

MsgBox "File has been imported. Now able to save and add contacts.", , "Alert"
End Sub

This imports the contacts and adds to a counter (i) each time.

Private Sub cmdSortA_Click()
Dim k As Integer
Dim q As Integer
Dim comp1, comp2 As Integer
Dim tempName, tempNum As String

If filename = "" Then
MsgBox "Please import a file before saving.", vbCritical, "ERROR" 'if user did not select a file
Exit Sub
End If

For x = lblEntry.LBound To i
For j = lblEntry.LBound To i - 1
k = 1
comp1 = ascend(lblName(j).Caption, k)
comp2 = ascend(lblName(j + 1).Caption, k)
If comp1 = comp2 Then
Do Until comp1 <> comp2
k = k + 1
comp1 = ascend(lblName(j).Caption, k)
comp2 = ascend(lblName(j + 1).Caption, k)
Loop
End If
If comp2 < comp1 Then
tempName = nam(j)
tempNum = phNum(j)
nam(j) = nam(j + 1)
phNum(j) = phNum(j + 1)
nam(j + 1) = tempName
phNum(j + 1) = tempNum
End If
Next j
Next x

For q = 1 To i
lblName(q).Caption = nam(q)
lblNumber(q).Caption = phNum(q)
Next
End Sub

k is reset each time the loop is started, so when the loop runs fresh only the first character changes. If the characters are the same, it executes a loop that checks each character until they aren't. It checks the next character by adding 1 to k (position of character). Lastly, it checks if the second is smaller than the first and switches the two if they are.

DougT
01-15-2008, 04:03 AM
Is there any particular reason you're using this method? I'd have thought just comparing one name to another and swapping them would have done.

dood
01-15-2008, 03:16 PM
Oh wow, I never knew you could sort by the string itself. Spent 3 classes on this, thanks rofl

dilettante
01-15-2008, 04:54 PM
You can automate most of this away by using ADO and Jet as well:
Option Explicit
'Reading and sorting a CSV text file via Jet 4.0 and extracting
'the Recordset data two ways.
'
'Requires reference to Microsoft ActiveX Data Objects 2.5 Library
'
'When run in the VB6 IDE the Recordset Open may fail the first
'time after the IDE has been opened or the source is changed.
'This is a VB6 bug introduced in SP3 and never fixed!
'
'Compiled programs work fine. When testing in the IDE you may
'have to re-run the program.

Private Const Filename = "commadelim.txt" 'Sample CSV file to read.

Private Sub Command1_Click()
Dim rs As New ADODB.Recordset
Dim I As Integer, J As Integer
Dim Data As Variant

rs.Open "SELECT * FROM [" & Filename & "] ORDER BY F1 ASC", _
"Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source='" & App.Path & "';" _
& "Extended Properties='Text;HDR=No;FMT=Delimited'", _
adOpenStatic, adLockReadOnly, adCmdText

'Using GetString() to simply display the data.
Text1.Text = "GetString():" & vbNewLine _
& rs.GetString(, , " ", vbNewLine)

rs.MoveFirst

'Using GetRows() to get the data into a 2-D array.
Data = rs.GetRows()
rs.Close
Text1.Text = Text1.Text & vbNewLine & "GetRows():" & vbNewLine
For J = 0 To UBound(Data, 2)
For I = 0 To UBound(Data, 1)
Text1.Text = Text1.Text & Data(I, J) & " "
Next I
Text1.Text = Text1.Text & vbNewLine
Next J
End Sub
The default field names are F1, F2, etc. if you have no schema.ini file in the same folder as the text files.

Often it is just as easy to use the data in the open Recordset rather than to copy it into arrays.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum