Hi Guys. I hope everyone in doing great today:-)

I have some problem, this coe does not produce mi in aplhabetical oreder.
I am using Heapsort as a sorting algo...and please help me

Hope to hear from you guys;-)

    Public Class MyForm
    Private MyTable As New DataTable
    Private MySortedTable As New DataTable
    Private CounterToMySortedTable As Integer
    Private Sub SwapSmallest()
        Dim ParentNode, ResultOfCompare As Integer
        Dim temp As String
        Try
            ParentNode = 0
            ResultOfCompare = StrComp(MyTable.Rows(ParentNode + 1)("ChildName"), MyTable.Rows(ParentNode + 2)("ChildName"))
            If ResultOfCompare = -1 Then
                temp = MyTable.Rows(ParentNode + 1)("ChildName")
                MyTable.Rows(ParentNode + 1)("ChildName") = MyTable.Rows(0)("ChildName")
                MyTable.Rows(0)("ChildName") = temp
            Else
                temp = MyTable.Rows(ParentNode + 2)("ChildName")
                MyTable.Rows(ParentNode + 2)("ChildName") = MyTable.Rows(0)("ChildName")
                MyTable.Rows(0)("ChildName") = temp
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub Promote(ByVal PromotedChild As String)
        Try
            MyTable.Rows(0)("ChildName") = PromotedChild
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub PutToSortedDataTable(ByVal id As Integer, ByVal EntryName As String)
        Try
            MySortedTable.Rows.Add(id, EntryName)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Function ArrangeInAlphabeticalOrder() As DataTable
        Dim i, Id As Integer
        Try
            For i = MyTable.Rows.Count - 1 To 0 Step -1
                Id += 1
                PutToSortedDataTable(Id, MyTable.Rows(0)("ChildName"))
                Promote(MyTable.Rows(i)("ChildName"))
                SwapSmallest()
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        ArrangeInAlphabeticalOrder = MySortedTable
    End Function
    Private Sub HeapifyMyTable()
        Dim Parent As Integer
        Dim ResultCompare, Last As Integer
        Dim temp As String
        Try
            Last = MyTable.Rows.Count - 1
            Parent = (Last - 1) / 2
            Do
                If MyTable.Rows.Count > 0 Then
                    ResultCompare = StrComp(MyTable.Rows(Parent)("ChildName"), MyTable.Rows(Last)("ChildName"))
                    If ResultCompare = 1 Then
                        temp = MyTable.Rows(Last)("ChildName")
                        MyTable.Rows(Last)("ChildName") = MyTable.Rows(Parent)("ChildName")
                        MyTable.Rows(Parent)("ChildName") = temp
                        Last = Parent
                        Parent = (Last - 1) / 2
                    End If
                End If
            Loop Until ResultCompare = -1 Or ResultCompare = 0
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub GetNameFromOldDataGridView()
        Dim EntryName As String
        Dim i As Integer
        Dim row As DataRow = MyTable.NewRow

        MyTable.Columns.Add("ChildNameID")
        MyTable.Columns.Add("ChildName")
        Try
            MySortedTable.Columns.Add("ChildNameID")
            MySortedTable.Columns.Add("ChildName")
            For i = 0 To OldDataGridView.Rows.Count - 1
                EntryName = OldDataGridView.Rows(i).Cells(1).Value
                MyTable.Rows.Add(i + 1, EntryName)
                HeapifyMyTable()
            Next
            MySortedTable = ArrangeInAlphabeticalOrder()
            NewDataGridView.DataSource = MySortedTable
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub MyForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ChildNameTableAdapter.Fill(TestDataSet.ChildName)
    End Sub
    Private Sub ArrangeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ArrangeButton.Click
        Call GetNameFromOldDataGridView()
    End Sub
End Class

`

I notice you're sending the sorted data to a new datagridview. Wouldn't it be easier to just sort the datagridview? It has methods built in designed to do this.

You might also want to look at the DataTable.Select method, which has a sort option and will return an array of DataRows that you can iterate through and add to your sorted table.

Yes @tinstaafl, it is really meant to have 2 data gridview, oldDataGridView and NewDataGridView, the old is unsorted while the new datagridview is sorted in alphabetical order....i like to use DataTable since it is a "in-memory" and it will have a faster performance when it comes to sorting in the memory...so, when data is sorted in the dataTable, i will transfer it to the newDataGridView....Thank you sir for your suggestion:-)

Anything else sir for suggestion or a way how to do it??..

Did you look at the link in my last post? It has a working example of how to sort a datatable.

where sir??..

The part that is blue and underlined and changes the mouse cursor to a hand when ther cursor is over it. Here it is again, DataTable.Select

Here's another sample for you:

    'Adds the columns to the new table
    MySortedTable.Columns.AddRange((From dc As DataColumn In MyTable.Columns
                                    Select New DataColumn(dc.ColumnName + "Sorted")).ToArray)
    'Adds the sorted rows to the new table.  sorted with the Select method of the DataTable
    'The first argument is a filter, I set it to any name that isn't empty
    'The second argument is the name of the column to sort by and the direction of the sort.  
    'This is set to sort by the "ChildName" column Ascending
    For Each row As DataRow In A.MyTable.Select("ChildName <> ''", "ChildName ASC")
        Dim temprow As DataRow = MySortedTable.NewRow
        temprow.ItemArray = row.ItemArray
        MySortedTable.Rows.Add(temprow)
    Next
    'Add the table's data to the datagridview
    NewDataGridView.DataSource = MySortedTable

wow!!! Thank you sir:-)...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.