带有主要,次要,第三标准的锯齿状阵列排序

时间:2016-01-21 14:19:08

标签: vb.net linq sorting multidimensional-array

免责声明:我作为程序员的背景仅限于1门C ++大学课程和业余爱好者vb.net宏编程。

我的目标是能够从BOM中获取大量数据,有条件地对其进行排序,然后返回包含文档名称的简单列表。

我在LINQ和iComparable上遇到了一些关于结构/字符串的好信息,但是我可以使用一些进一步的指导来实现这一点。

基本上它将是一个多级排序,其中包含一些逻辑:

-> Put objects with 'Drawing' = True first.
-> Sort then by ComponentDefinition/Secondary document desciber;
    (ie; assemblies and weldments, then plate, then structural steel, then hardware).
   -> Sort Assemblies and weldments by Renamed vs Matl'l Spec then by weight
    -> Sort Structural by type, then by size
    -> Sort hardware by nomial size, then by type, then by thread unc/unf, then by length
    -> Put the remainder last (ie; virtual components.

从我所读过的内容来看,如果能让它工作,似乎我可以通过多套linq来实现这一目标,

或者我可以使用比较功能,只是将每一行完整地与下一行比较(正如我对它的理解所暗示的那样?)

所以在一天结束时,我想要一个列表来吐出类似的东西:

1) Drawing - Weldment - Named - Weight
2) Drawing - Assembly - Named - Weight
3) Drawing - Assembly - Unnamed - Weight
4) Drawing - Weldment - unnamed - weight
5) Drawing - Plate - 1" Thick x 48 sq in.
6) Drawing - Plate - 1" Thick x 36 sq in.
7) No Drawing - Plate - 1" Thick X 52 in
8) No Drawing - Plate - 1/2" thick X 52 in
9) No Drawing - 1/2" Bolt - UNC - 3"
10) No Drawing - 1/2" Bolt - UNC - 2"
11) No Drawing - 1/2" Nut - UNC
12) No Drawing - 1/2" Washer
13) No Drawing - 1/4" Bolt - UNC - 2"
14) No Drawing - Virtual Component 

通过使用Case 0作为下一个级别,并且进行树排序似乎是正确的方法,只要我可以使基本树工作。 这样,我只根据它们在前一种类型中的结果来对项目进行排序。 (如果我将组件与硬件进行比较,则无需进一步,因为在绘制/无绘制排序之后,我的排序的次要级别)

这是我已经得到的,如果我能弄清楚如何使这个工作,我想我可能会从这里继续

'Jagged Array Sorting with Tag Array
Sub Main()

Dim oBOMRowCount As Integer = 4

Dim oBOMFactorArray As Double(oBOMRowCount-1)(){}
    oBOMFactorArray(0) = {"2", "5"}
    oBOMFactorArray(1) = {"1", "5"}
    oBOMFactorArray(2) = {"1", "2"}
    oBOMFactorArray(3) = {"2", "7"}

Dim tagArray() As Integer = {0, 1, 2, 3}

' Initialize the comparer and sort
Dim myComparer As New RectangularComparer(oBOMFactorArray)
Array.Sort(tagArray, myComparer)

Dim i As Integer
For j = 0 To 3
    oLine = ""
    For i = 0 To 1
        If oLine = ""
            oLine = "oList[" & (oBOMFactorArray(j)(i)) & "]"
        Else
            oLine = oLine & "[" & (oBOMFactorArray(j)(i)) & "]"
        End If
    Next
    oStr = oStr & vbLf & oLine
Next
MsgBox(oStr)

End Sub()

Class RectangularComparer
Implements IComparer
' maintain a reference to the 2-dimensional array being sorted

Private sortArray(,) As Integer

' constructor initializes the sortArray reference
Public Sub New(ByVal theArray(,) As Double)
    sortArray = theArray
End Sub

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
    ' x and y are integer row numbers into the sortArray
    Dim i1 As Integer = DirectCast(x, Integer)
    Dim i2 As Integer = DirectCast(y, Integer)

    Select Case sortArray(i1, 0).CompareTo(sortArray(i2, 0))
        Case -1
        Case 1
        Case 0
                Return sortArray(i1, 1).CompareTo(sortArray(i2, 1))
    End Select
    ' compare the items in the sortArray
End Function
End Class

0 个答案:

没有答案