?
Solved

Sort array (Numbers)

Posted on 2005-03-02
8
Medium Priority
?
319 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
[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
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

777 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