Sorting dimensions in an array

I am writing a report to the listing window containing a bunch of dimensions. I want to have this list sorted by the AnnotationOrigin distance from the sheet origin instead of the jump around order that they were created in. What would be the preferred method to do this? My current method adds strings to a list, sorts the list, and then calls the dimensions in order from the list. I am pretty sure this is not the most efficient method.

Dim Dimensions As Annotations.Dimension()
Dimensions = DisplayPart.Dimensions.ToArray()

For Each Dimension As Annotations.Dimension In Dimensions

Dim p1 As Point3d = Dimension.AnnotationOrigin
Dim dist As Double = Math.Sqrt((p1.X) ^ 2 + (p1.Y) ^ 2 + (p1.Z) ^ 2)
If dist > 1000 Then
DimLocations.Add(dist & "-" & Dimension.Tag)
Else If dist < 1000 And dist > 100
DimLocations.Add("0" & dist & "-" & Dimension.Tag)
Else If dist < 100
DimLocations.Add("00" & dist & "-" & Dimension.Tag)
End If
End If
Next

DimLocations.Sort()

For Each distance As String In DimLocations
Dim result() As String = Split(distance, "-")
Dim test As Tag = result(1)
Dim theDim As Annotations.Dimension
theDim = DirectCast(NXOpen.Utilities.NXObjectManager.Get(test), Annotations.Dimension)
'do stuff with the dimensions in numeric order
Next

Here is some code to demonstrate sorting a list by a function of your choosing.


Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()
Dim workPart As Part = theSession.Parts.Work

Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

Dim myDimensions As New List(Of Annotations.Dimension)

'add all dimensions to a list
For Each tempDim As Annotations.Dimension In workPart.Dimensions
myDimensions.Add(tempDim)
Next

'sort the dimensions
myDimensions.Sort(AddressOf CompareByDistance)

'write out sorted list
lw.WriteLine("Dimensions sorted by distance from origin (closest to furthest)")
lw.WriteLine("---------------------------------------------------------------")
For Each tempDim As Annotations.Dimension In myDimensions
lw.WriteLine("tag: " & tempDim.Tag.ToString)
Dim dimMainText() As String
Dim dimDualText() As String
tempDim.GetDimensionText(dimMainText, dimDualText)
lw.WriteLine("main text: " & dimMainText(0))
lw.WriteLine("distance: " & DimensionDistance(tempDim).ToString)
lw.WriteLine("")
Next

'reverse the list
myDimensions.Reverse()

'write out reverse sorted list
lw.WriteLine("Dimensions sorted by distance from origin (furthest to closest)")
lw.WriteLine("---------------------------------------------------------------")
For Each tempDim As Annotations.Dimension In myDimensions
lw.WriteLine("tag: " & tempDim.Tag.ToString)
Dim dimMainText() As String
Dim dimDualText() As String
tempDim.GetDimensionText(dimMainText, dimDualText)
lw.WriteLine("main text: " & dimMainText(0))
lw.WriteLine("distance: " & DimensionDistance(tempDim).ToString)
lw.WriteLine("")
Next

End Sub

Public Function DimensionDistance(ByVal theDim As Annotations.Dimension) As Double

Dim dimLocation As Point3d
dimLocation = theDim.AnnotationOrigin

Return Math.Sqrt(dimLocation.X ^ 2 + dimLocation.Y ^ 2)

End Function

Private Function CompareByDistance(ByVal x As Annotations.Dimension, ByVal y As Annotations.Dimension) As Integer

Dim distX As Double = DimensionDistance(x)
Dim distY As Double = DimensionDistance(y)

If distX > distY Then
Return 1
End If

If distX < distY Then
Return -1
End If

If distX = distY Then
Return 0
End If

End Function

Public Function GetUnloadOption(ByVal dummy As String) As Integer

'Unloads the image when the NX session terminates
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

'----Other unload options-------
'Unloads the image immediately after execution within NX
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

'Unloads the image explicitly, via an unload dialog
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Explicitly
'-------------------------------

End Function

End Module