Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

Sorting Structure with 0 Value

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

0
jhart710
Asked:
jhart710
  • 5
  • 3
  • 3
2 Solutions
 
CodeCruiserCommented:
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
0
 
jhart710Author 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?
0
 
CodeCruiserCommented:
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)))
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jhart710Author Commented:
Would I put this in my main code or would this be part of my structure definition?
0
 
CodeCruiserCommented:
You can put it where you would normally call the Sort.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

0
 
jhart710Author 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

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I think it has to do with the fact that you are using a Structure and the Part_Number field is actual Nothing causing sort problems.

If you change to a Class and provide a default blank string value then it behaves differently:
Public Class Tool
        Implements IComparable(Of Tool)

        Public Part_Number As String = ""
        Public Serial_Number As String
        Public QTY_Ready As Integer
        Public QTY_Dull As Integer
        Public QTY_Crib88 As Integer
        Public QTY_Hold As Integer
        Public Threshold As Integer
        Public Severity As Decimal
        Public Tool_Family As String
        Public Priority As Integer

        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

    End Class

Open in new window

0
 
jhart710Author 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.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
When you change to a class, you have to place an instance in each slot before you use it:

    For x = 0 To 10
        Tools(x) = New Tool()
        Tools(x).XXX = YYY
        ...

You'd need to place an instance in the other 1990 slots as well though!

Do you really need a 2000 element list?...or just one big enough to hold your items?

For the latter, you can use a List instead:

    Public Tools As New List(Of Tool)

Then you Add() things to it:

    For x = 0 To 10
        Dim T As New Tool
        T.XXX = YYY
        ' etc...

        Tools.Add(T)
    Next
0
 
jhart710Author 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.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 5
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now