array structure sort?

SI03

Member
Joined
Mar 30, 2006
Messages
12
Programming Experience
Beginner
I am fairly new to vb.net and was wanting to make a bubble sort from this information. From searching the net I found some but still do not understand how to do it with what I have.
Here is all of the code that I have and guidance on where to start to do a bubble sort ? I have a menu with each of the structure names in it and would like to be able to sort the array when it is displayed by each of those elements.Am I missing something with the structure to be able to do this?
VB.NET:
    Structure MovieInfo
        <VBFixedString(20)> Public Title As String
        <VBFixedString(4)> Public Year As Integer
        <VBFixedString(4)> Public Price As Integer
        Public sngPrice As Single
    End Structure

    Public udtMovie As MovieInfo
    Public objMovieArray(0) As Object

    Private Sub btnAddMovie_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddMovie.Click
        Try
            udtMovie.Title = txtTitle.Text
            udtMovie.Year = txtYear.Text
            udtMovie.Price = txtPrice.Text
            udtMovie.sngPrice = Val(txtPrice.Text)
            Dim intSize As Integer
            intSize = UBound(objMovieArray)
            ReDim Preserve objMovieArray(intSize + 1)
            objMovieArray(intSize) = udtMovie.Title & vbTab & udtMovie.Year & vbTab & udtMovie.sngPrice
            MsgBox("The Following Movie was added to array:" & vbCrLf & objMovieArray(intSize), MsgBoxStyle.Information, "Record added")
            'btnViewArray.Enabled = True
            txtTitle.Clear()
            txtYear.Clear()
            txtPrice.Clear()
        Catch ex As Exception
            MsgBox("Error occured in application", MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub btnViewArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnViewArray.Click
        Try
            Dim intCounter As Integer
            Dim intSize As Integer
            intSize = UBound(objMovieArray) - 1
            lstArray.Items.Clear()

            For intCounter = 0 To intSize
                lstArray.Items.Add(objMovieArray(intCounter))
            Next intCounter
        Catch ex As Exception
            MsgBox("Error occuried while trying to view the array", MsgBoxStyle.Critical)
        End Try
    End Sub
 
Last edited by a moderator:
It is obvious to me that I am lacking something to understand how to do this. I tried adding the j and j-1 to the if statement but guess that was not it, and tried other variations of the if statement to no avail.
 
OK, there is no reason to be using Array.IndexOf anyway. You need to compare the values of the elements at index j and j-1. You know how to get those values because you're already doing it inside the loop. Just take those elements, put them in the If statement and compare them.

I'm afraid that that is as much help as I can give. I've made it as clear as I can without actually writing the code and I'm not going to do that as it's your homework.
 
Are you saying it is suppose to be like this? maybe it doesn't work like it is suppose to because it is an object? tells me it is not a valid operator for the type. I do thank you for trying to get me to understand . I just seem not to be able to comprehend this part.
VB.NET:
[SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] SortYear([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] objMovieArray [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#008000]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] temp [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 1 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] objMovieArray.GetUpperBound(0) [/SIZE][SIZE=2][COLOR=#0000ff]Step[/COLOR][/SIZE][SIZE=2] 1
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] j [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = i [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] 1 [/SIZE][SIZE=2][COLOR=#0000ff]Step[/COLOR][/SIZE][SIZE=2] -1
[/SIZE][SIZE=2][COLOR=#008000]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] objMovieArray(j) > objMovieArray(j - 1) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]temp = objMovieArray(j)
objMovieArray(j) = objMovieArray(j - 1)
objMovieArray(j - 1) = temp
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2] j
[/SIZE][SIZE=2][COLOR=#0000ff]Next i
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
 
That's basically it but, as the error says, there is no ">" operator for the objects you are comparing. From what you've posted previously I assume that it is the sYear field that you want to order them by, so don't compare the objects themselves. Rather compare their sYear fields. If I wanted to put a group of people into a line in alphabetical order I wouldn't compare each person. I would compare the name of each person. You need to do essentially the same thing. Instead of comparing objMovieArray(j) and objMovieArray(j - 1) you need to compare the sYear field of each of them.
 
Is this along the lines as what you are trying to tell me, but I have yet to grasp? If so then I haven't figured out how to have the j and j-1 in there so it compares the different values.

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] StrComp(udtMovie.sYear, udtMovie.sYear, CompareMethod.Text) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE]
 
You can use StrComp but I'd recommend against it. If you want to stipulate a case-insensitive comparison then use String.Compare. If you don't want it case-sensitive then you can just use the standard comparison operators ">" and "<".

Having said that, take a look at the code you posted. You're supposed to be comparing two different things, but that code is comparing something to itself. You are comparing the sYear property of udtMovie with the sYear property of udtMovie. You're supposed to be comparing the sYear properties of the two array elements.
 
I tried that and it gave me an error about the sYear that is why I said I was lost. I am sorry I took up your time.
 
OK, I lied. It wasn't my last post. I just looked back at your earlier posts and I realised that you're passing objMovieArray to the method as an Object. That's your problem I would say. There's no reason to use such a general type. The object you're passing is an array of MovieInfo objects so declare the argument as an array of MovieInfo objects. This:
VB.NET:
[SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] SortYear([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] objMovieArray [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff] Object[/COLOR][/SIZE][SIZE=2])[/SIZE]
becomes this:
VB.NET:
[SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] SortYear([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] objMovieArray [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff] MovieInfo()[/COLOR][/SIZE][SIZE=2])[/SIZE]
Now that the method knows that it's an array of MovieInfo objects you can treat it like one, i.e. get elements by index and then get the properties of those elements. Also, in one of your early posts you indicated that the field was named 'Year', not 'sYear', yet in later code you were using 'sYear'. You need to make your mind up on that.
 
Back
Top