Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# Sort array (Numbers)

Posted on 2005-03-02
Medium Priority
333 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

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

XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
###### Suggested Courses
Course of the Month13 days, 9 hours left to enroll