Solved

# Sort array (Numbers)

Posted on 2005-03-02
Medium Priority
319 Views
Hello

I have an array and I want to sort it.
The array contains several numbers, e.g.:

54
43
72
34
2
6
74
23

Now I want to sort it, but if I am using array.sort(array) then it is sorting like this:

2
24
34
43
54
6
72
74

It is only sorting with the first number - how can I solve this?

Thanks

Sven
0
Question by:Ruttensoft
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 34

Expert Comment

ID: 13443825
your array doesn't contain numbers it contains text.  How are you inserting the values in the array?
0

Author Comment

ID: 13443869
Dim ff = ListBox1.Items.Count - 1
Dim arrayf(ff) As String
For i = 0 To ff
arrayf(i) = ListBox2.Items.Item(i) & "####" & ListBox1.Items.Item(i)
Next
arrayf.Sort(arrayf)
ListBox1.Items.Clear()
ListBox2.Items.Clear()
For i = 0 To ff
Dim ss() As String
ss = Split(arrayf(i), "####")
Next
0

LVL 2

Expert Comment

ID: 13443879
Bri is correct, when you insert the values into the array make sure you are inserting integers and not strings.
0

LVL 2

Expert Comment

ID: 13444017
Quick and dirty....

Dim ff = ListBox1.Items.Count - 1
Dim arrayf(ff) As String
For i As Integer = 0 To ff
arrayf(i) = StrDup(4 - Len(ListBox2.Items.Item(i)), "0") & ListBox2.Items.Item(i) & "####" & StrDup(4 - Len(ListBox1.Items.Item(i)), "0") & ListBox1.Items.Item(i)
Next
arrayf.Sort(arrayf)
ListBox1.Items.Clear()
ListBox2.Items.Clear()
For i As Integer = 0 To ff
Dim ss() As String
ss = Split(arrayf(i), "####")
Next
0

LVL 2

Accepted Solution

lordicarus earned 75 total points
ID: 13444072
Better way...

Sub SortListBox(ByRef lbControl As ListBox)
Dim iItems As Integer = lbControl.Items.Count - 1
Dim itemArray(iItems) As Integer
For i As Integer = 0 To iItems
itemArray(i) = lbControl.Items.Item(i)
Next
Array.Sort(itemArray)
lbControl.Items.Clear()
For Each iItem As Integer In itemArray
Next
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SortListBox(ListBox1)
SortListBox(ListBox2)
End Sub
0

LVL 12

Expert Comment

ID: 13444117
lordicarus... I might be wrong but I think the ListBox1 and ListBox2 item's are linked to each other, so that when you move and item in ListBox2, they intend the opposite item in ListBox1 to move to the same position... hence the line ListBox2.Items.Item(i) & "####" & ListBox1.Items.Item(i).  You're previous solution maintains this except you forgot to trim off the leading zeros

Dim ff = ListBox1.Items.Count - 1
Dim arrayf(ff) As String
For i As Integer = 0 To ff
arrayf(i) = StrDup(4 - Len(ListBox2.Items.Item(i)), "0") & ListBox2.Items.Item(i) & "####" & StrDup(4 - Len(ListBox1.Items.Item(i)), "0") & ListBox1.Items.Item(i)
Next
arrayf.Sort(arrayf)
ListBox1.Items.Clear()
ListBox2.Items.Clear()
For i As Integer = 0 To ff
Dim ss() As String
ss = Split(arrayf(i), "####")
Next
0

LVL 12

Expert Comment

ID: 13444133
I may be wrong and that the listboxes can be sorted independantly, in which case your second piece of code will work, but should the list's be linked, you'll need to stick with the first kind (or a completely different piece of code)
0

LVL 12

Expert Comment

ID: 13444142
Look's like the could be sorted independantly :P
0

## Featured Post

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
###### Suggested Courses
Course of the Month12 days, 13 hours left to enroll