Problem with sorting

xpertwinner

Member
Joined
Dec 26, 2008
Messages
24
Programming Experience
Beginner
I have a program, where I have created an object type called "Modulo". This class has certain atributes, shuch as an ID, a name, a position and others.

What I have is a submarine, which is a list of those modules. I want to read positions from a txt file and then order the submarine according to those positions.

What is happening is that the positions are well read, the sorting of the positions is correctly done, but the "names" of the modules don't follow!
For example, when I get 2 0 1 from the file, the program should print:

0 B
1 C
2 A

but instead it prints

0 B
1 A
2 C

The code is as follows:


VB.NET:
        Dim teste As New List(Of Modulo)

        teste.Add(New Modulo(1, "A", 100))

        teste.Add(New Modulo(2, "B", 150))

        teste.Add(New Modulo(3, "C", 100))

        Try

            Dim instance As StreamReader = New StreamReader("C:\Documents and Settings\Hugo\Os meus documentos\permut_table\permut_table.txt")
            Dim LineofText As String
            Dim size = teste.Count()
            Dim i As Integer
            Dim limit As Integer
            Dim submarino_de_calculo As New List(Of Modulo)

            limit = Factorial(size)

            i = 0
            'if you have n elements, read the n! first lines from the element 10-n until the end

            Do While (instance.Peek <> -1 And i < limit)

                LineofText = instance.ReadLine()

                Dim positions_temp() As String
                Dim positions() As String
                Dim cSplitAt(1) As Char
                cSplitAt(0) = Chr(32)
                positions_temp = LineofText.Split(cSplitAt, StringSplitOptions.RemoveEmptyEntries)
                positions = positions_temp.GetRange(10 - size, size)

                Dim k As Integer

                MsgBox("posicao" & i)
                For k = 0 To positions.Count() - 1
                    MsgBox(positions(k))
                Next

                submarino_de_calculo = teste

                Dim j As Integer

                For j = 0 To positions.Count() - 1
                    submarino_de_calculo(j).Posicao = positions(j)
                Next

                submarino_de_calculo.Sort(AddressOf CompareModulos)

                MsgBox("Positions after ordered")

                Dim a As Integer
                For a = 0 To submarino_de_calculo.Count - 1
                    MsgBox(submarino_de_calculo(a).Designacao)
                Next

                i += 1
            Loop

        Catch P As Exception
            MsgBox("file not found")
        End Try



    Public Function CompareModulos(ByVal m1 As Modulo, ByVal m2 As Modulo)

        Return m1.Posicao.CompareTo(m2.Posicao)

    End Function


Thanks in advance,

Hugo Sousa Pinto
 
Last edited by a moderator:

Latest posts

Back
Top