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
8
Medium Priority
?
333 Views
Last Modified: 2011-09-20
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
Comment
Question by:Ruttensoft
8 Comments
 
LVL 34

Expert Comment

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

Author Comment

by:Ruttensoft
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), "####")
            ListBox1.Items.Add(ss(0))
            ListBox2.Items.Add(ss(1))
        Next
0
 
LVL 2

Expert Comment

by:lordicarus
ID: 13443879
Bri is correct, when you insert the values into the array make sure you are inserting integers and not strings.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 2

Expert Comment

by:lordicarus
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), "####")
            ListBox1.Items.Add(ss(0))
            ListBox2.Items.Add(ss(1))
        Next
0
 
LVL 2

Accepted Solution

by:
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
            lbControl.Items.Add(iItem)
        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

by:S-Twilley
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), "####")
            ListBox1.Items.Add(ss(0).TrimStart("0"))
            ListBox2.Items.Add(ss(1).TrimStart("0"))
        Next
0
 
LVL 12

Expert Comment

by:S-Twilley
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

by:S-Twilley
ID: 13444142
Look's like the could be sorted independantly :P
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: jpaulino
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

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question