We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Sorting Structure with 0 Value

jhart710
jhart710 asked
on
Medium Priority
309 Views
Last Modified: 2012-05-11
I have an array of a structure that I am trying to sort.  It sorts everything fine except when the sort against value is 0.  It simply omits those.  I have attached a code snippet of the structure and how i am using it.  Any suggestions?

   
Structure Tool
        Implements IComparable(Of Tool)

        Dim Part_Number As String
        Dim Serial_Number As String
        Dim QTY_Ready As Integer
        Dim QTY_Dull As Integer
        Dim QTY_Crib88 As Integer
        Dim QTY_Hold As Integer
        Dim Threshold As Integer
        Dim Severity As Decimal
        Dim Tool_Family As String
        Dim Priority As Integer

        Public Function CompareTo(ByVal other As Tool) As Integer Implements System.IComparable(Of Tool).CompareTo
            Return Me.Severity.CompareTo(other.Severity)
        End Function
    End Structure


    Public Tools(2000) As Tool

'Populate Data from DB


        Array.Sort(Tools)
        Array.Reverse(Tools)

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Try changing this

Public Function CompareTo(ByVal other As Tool) As Integer Implements System.IComparable(Of Tool).CompareTo
            Return Me.Severity.CompareTo(other.Severity)
        End Function

to


Public Function CompareTo(ByVal other As Tool) As Integer Implements System.IComparable(Of Tool).CompareTo
    Dim Res As integer
    If Me.Severity < Other.Severity Then
       Res = -1
    ElseIf Me.Severity = Other.Severity Then
       Res = 0
    Else
       Res = 1
    End If
    Return Res
End Function

Author

Commented:
That did not fix the problem, but I did then find the actual problem.  The 0 value items are in the array, they  are just not sorted with the rest of the data.  I currently have a 2000 element Tools array.  I am only using 300 positions currently.  After my sorting positions 0 - 257 are populated and sorted correctly.  The 0 value items are located at positions 723-736, 740-752, and 754-768.  I think I need a 2 level sort where I sort by severity and then part number.  How would I accomplish that?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Try this(example so amend as required)

ProjectList.Sort(Function(p1 As ProjectsInfo, p2 As ProjectsInfo) IIf(p1.ProjectStatus.CompareTo(p2.ProjectStatus) <> 0, p1.ProjectStatus.CompareTo(p2.ProjectStatus), p1.DaysOverDue.CompareTo(p2.DaysOverDue)))

Author

Commented:
Would I put this in my main code or would this be part of my structure definition?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
You can put it where you would normally call the Sort.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
I still don't see how the 0 (zero) value items are ending up in those positions (not being sorted with the rest)...can you show more complete code?

"I think I need a 2 level sort where I sort by severity and then part number.  How would I accomplish that?"

Change your CompareTo() function to:
Public Function CompareTo(ByVal other As Tool) As Integer Implements System.IComparable(Of Tool).CompareTo
            Dim result As Integer = Me.Severity.CompareTo(other.Severity)
            If result = 0 Then
                result = Me.Part_Number.CompareTo(other.Part_Number)
            End If
            Return result
        End Function

Open in new window

Author

Commented:
First Sub is a test routine
Second part is my structure

array is 2000 elements
Public Tools(2000) as Tool
Public Sub PopulateTools2()
        Dim x As Integer
        For x = 0 To 10
            Tools(x).Part_Number = x.ToString("00000000")
            Tools(x).Serial_Number = x.ToString("00000000")
            Tools(x).QTY_Ready = 10
            Tools(x).QTY_Dull = 10
            Tools(x).QTY_Crib88 = 2
            Tools(x).QTY_Hold = 0
            Tools(x).Threshold = 8
            If x Mod 2 = 0 Then
                Tools(x).Severity = 0.5
            Else
                Tools(x).Severity = 0.8
            End If
            If x = 3 Or x = 4 Then Tools(x).Severity = 0
            Tools(x).Tool_Family = "A"
            Tools(x).Priority = 1
        Next
        Array.Sort(Tools)
        Array.Reverse(Tools)
        For x = 0 To 1999
            If Tools(x).Part_Number <> "" Then
                If Tools(x).Severity = 0 Then
                    MsgBox(x)
                End If
            End If
        Next
    End Sub


    Structure Tool
        Implements IComparable(Of Tool)

        Dim Part_Number As String
        Dim Serial_Number As String
        Dim QTY_Ready As Integer
        Dim QTY_Dull As Integer
        Dim QTY_Crib88 As Integer
        Dim QTY_Hold As Integer
        Dim Threshold As Integer
        Dim Severity As Decimal
        Dim Tool_Family As String
        Dim Priority As Integer

        Public Function CompareTo(ByVal other As Tool) As Integer Implements System.IComparable(Of Tool).CompareTo
            'Return Me.Severity.CompareTo(other.Severity)
            Dim result As Integer = Me.Severity.CompareTo(other.Severity)
            If result = 0 Then
                If Not other.Part_Number = Nothing Then
                    result = other.Part_Number.CompareTo(Me.Part_Number)
                End If
            End If
            Return result

        End Function
    End Structure

Open in new window

High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Now I get a NullReferenceException anytime I attempt to populate the Part_Number field.  I am new to structures and classes.  Can you point me in the right direction on this?  Thanks.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
I left my structure alone.  I modified my Tools array to be a list instead and now my sorting works.  I'm sure it is because of the Nothing value of Part_Number like you said.  Thank you much for the help.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.