# Need Help with my Array Sorting Code

Page 1 of 1

## 5 Replies - 1117 Views - Last Post: 15 October 2009 - 05:34 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=130708&amp;s=93097361350c87cc247d238718b42116&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jackhellstorm27

Reputation: 0
• Posts: 3
• Joined: 08-October 09

# Need Help with my Array Sorting Code

Posted 08 October 2009 - 07:22 AM

Used Listbox named List1 and Command1
This code creates a listbox of 100 random numbers in an array
I'm now trying to get it to sort the listbox but it won't work I
always get the message type mismatch plz help

```Dim intMYRANDOMS(100) As Integer
Dim intARRAYSIZE As Integer
Dim intSWITCH1 As Integer
Dim intSWITCH2 As Integer
Dim nC As String
Randomize
For x = 1 To 100 Step 1
intMYRANDOMS(x) = Int(Rnd * 100)
Next x
End Sub
Private Sub Command1_Click()
Do Until nC = intARRAYSIZE - 1 ' you can change this to -2 and the bonus number will remain at the end
If intMYRANDOMS(nC) > intMYRANDOMS(nC + 1) Then

MsgBox intMYRANDOMS(nC) & "  " & intMYRANDOMS(nC + 1)
intSWITCH1 = intMYRANDOMS(nC)
intSWITCH2 = intMYRANDOMS(nC + 1)
intMYRANDOMS(nC) = intSWITCH2
intMYRANDOMS(nC + 1) = intSWITCH1
If nC = 0 Then
nC = nC + 1
Else
nC = nC - 1
End If
Else
nC = nC + 1
End If

Loop

End Sub
```

Mod Edit: When you are posting code please use code tags like this.

Is This A Good Question/Topic? 0

## Replies To: Need Help with my Array Sorting Code

### #2 magius96

Reputation: 75
• Posts: 834
• Joined: 15-April 09

## Re: Need Help with my Array Sorting Code

Posted 08 October 2009 - 07:33 AM

Please put your code inside the [ code] [ /code] tags, it makes it easier to read.

Also, what line is it giving you the error on?

Here's part of your problem though:Dim nC As String
You're declaring nC as a string, but you use it as an Integer. Perhaps you meant to declare nC as an Integer?

### #3 jackhellstorm27

Reputation: 0
• Posts: 3
• Joined: 08-October 09

## Re: Need Help with my Array Sorting Code

Posted 08 October 2009 - 09:50 PM

I believe that nC must be a string, really I think my problem could be in the area of declaring what intARRAYSIZE is... the line that troubles me is
``` Do Until nC = intARRAYSIZE - 1
```

This post has been edited by jackhellstorm27: 08 October 2009 - 09:52 PM

### #4 magius96

Reputation: 75
• Posts: 834
• Joined: 15-April 09

## Re: Need Help with my Array Sorting Code

Posted 09 October 2009 - 04:54 AM

intMYRANDOMS(nC)
intMYRANDOMS(nC + 1)

These lines just scream that nC must be an integer.

Heck even Do Until nC = intARRAYSIZE - 1 screams it too.
But hey, as my signature suggests, I'm not an expert.

### #5 jackhellstorm27

Reputation: 0
• Posts: 3
• Joined: 08-October 09

## Re: Need Help with my Array Sorting Code

Posted 12 October 2009 - 10:56 AM

You were correct nC is an integer but it still hasn't fixed my first problem, type mismatch
```Dim intMYRANDOMS(100) As Integer
Dim intARRAYSIZE As Integer
Dim intSWITCH1 As Integer
Dim intSWITCH2 As Integer
Dim nC As Integer
Randomize
For x = 1 To 100 Step 1
intMYRANDOMS(x) = Int(Rnd * 100)
Next x
End Sub
Private Sub Command1_Click()
Do Until nC = intARRAYSIZE - 1 ' you can change this to -2 and the bonus number will remain at the end
If intMYRANDOMS(nC) > intMYRANDOMS(nC + 1) Then

MsgBox intMYRANDOMS(nC) & "  " & intMYRANDOMS(nC + 1)
intSWITCH1 = intMYRANDOMS(nC)
intSWITCH2 = intMYRANDOMS(nC + 1)
intMYRANDOMS(nC) = intSWITCH2
intMYRANDOMS(nC + 1) = intSWITCH1
If nC = 0 Then
nC = nC + 1
Else
nC = nC - 1
End If
Else
nC = nC + 1
End If

Loop

End Sub

```

This post has been edited by jackhellstorm27: 12 October 2009 - 10:56 AM

### #6 thava

• D.I.C Lover

Reputation: 180
• Posts: 1,607
• Joined: 17-April 07

## Re: Need Help with my Array Sorting Code

Posted 15 October 2009 - 05:34 PM

i think
still you are not in the right way

seems there is no sorting enable in your code