0

I'm currently translating a VBA project into VB.NET. And I have a little issue with the sorting of a datatable.

My table look like :

... | ... | Firstname3 | Name3 | Pay3 | Firstname1 | Name1 | Pay1 | Firstname2 | Name2 | Pay2 |...
... | ... | Firstname2 | Name2 | Pay2 | Firstname3 | Name3 | Pay3 | Firstname1 | Name1 | Pay1 |...

And so on... I export the 56 columns needed from the datatable into an array and try to sort it horizontaly on the Name. I did it that way in VBA :

Public Sub SortTable(ByRef aggTab(,) As Object, ByVal columnToSortOn As Integer, ByVal lowerValue As Byte,
                    ByVal upperValue As Byte)

    Dim ref As Object = aggTab((lowerValue + upperValue) \ 2, columnToSortOn)
    Dim refLowerValue As Byte = lowerValue
    Dim refUpperValue As Byte = upperValue
    Dim temp As Object

    Do
        Do While aggTab(refLowerValue, columnToSortOn) < ref
            refLowerValue = refLowerValue + 1
        Loop
        Do While ref < aggTab(refUpperValue, columnToSortOn)
            refUpperValue = refUpperValue - 1
        Loop
        If refLowerValue <= refUpperValue Then
            For i = LBound(aggTab, 2) To UBound(aggTab, 2)
                temp = aggTab(refLowerValue, i)
                aggTab(refLowerValue, i) = aggTab(refUpperValue, i)
                aggTab(refUpperValue, i) = temp
            Next i
            refLowerValue = refLowerValue + 1 : refUpperValue = refUpperValue - 1
        End If
    Loop While refLowerValue <= refUpperValue

    If refLowerValue < upperValue Then Call SortTable(aggTab, columnToSortOn, refLowerValue, upperValue)
    If lowerValue < refUpperValue Then Call SortTable(aggTab, columnToSortOn, lowerValue, refUpperValue)
End Sub

But when I convert the code into VB.NET it doesn't work properly. Does anyone can explain me why? Because in excel it work perfectly.

3
  • 1
    I don't speak french, but what exactly is not working properly. It seems you sorting something your self, which can be easely done using vb.net's collections. You just throw in the stuff, and VB sorts it for you... msdn.microsoft.com/en-us/library/f7fta44c.aspx Commented Aug 13, 2013 at 8:39
  • No need to speek french you know ;) The converted code is sorting but absoltly not like it have to... There is some column missing... I'm gonna look in the vb's collections then. Commented Aug 13, 2013 at 8:59
  • If you can, you should convert aggTab to a list of class. It would make your life must easier. Commented Aug 13, 2013 at 12:52

1 Answer 1

1

Take this example;

    Dim openWith As New SortedDictionary(Of String, String)

    ' Add some elements to the dictionary. There are no  
    ' duplicate keys, but some of the values are duplicates.
    openWith.Add("txt", "notepad.exe")
    openWith.Add("bmp", "paint.exe")
    openWith.Add("dib", "paint.exe")
    openWith.Add("rtf", "wordpad.exe")

    For Each Ext As KeyValuePair(Of String, String) In openWith
        TextBox1.AppendText(Ext.Key & " " & Ext.Value & vbCrLf)
    Next

This wil automaticly sort the 'extension'.

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

1 Comment

That is exactly the one I took after your previous comment. Thanks again.

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.