0
  Private Sub bttSort_Click(sender As Object, e As EventArgs) Handles bttSort.Click
        DataView1.Rows.Clear()
        Dim i As Integer
        Dim j As Integer
        Dim temp As Integer
        For i = 0 To list1.Length - 1
            For j = (i + 1) To list1.Length - 1
                If list1(i) > list1(j) Then
                    temp = list1(i)
                    list1(i) = list1(j)
                    list1(j) = temp
                End If
            Next
        Next
        DataView1.Rows.Clear()
        For m As Integer = 0 To list1.Length - 1
            DataView1.Rows.Add(list1(m))
        Next
    End Sub

I entered a string(1,3,5,6,7,113,23,62) After running the code above, the output should be sorted as (1, 3, 5, 6, 7, 23, 62, 113). But my out put was (1, 113, 23, 3, 5, 6, 62, 7). I have no idea, hope someone can help me. Thank you.

1 Answer 1

1

The will onyl get the output you want working with int, long, double.. but with string, they sort lexycographically, which means..

1, 113, 23, 3, 5, etc....

all the 1's first.. then 2's... etc... ;)

Convert all the elements to integers, and try again! ;)

You will get the expected output! ;)

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the answer, but could you please show me how to do that. I have tried "CInt", "Array.ConvertAll"... but nothing is working.
I think that if you create a int array, and then for example using a for loop in the string array, go copying all the values using CInt to the new int array... it will solve your problem ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.