View Single Post
  #5   Report Post  
Ron Rosenfeld
 
Posts: n/a
Default Sorting IP Address

On Wed, 19 Oct 2005 07:00:07 -0700, "ggant"
wrote:

How do I sort fields that contain IP numbers in order? ex. Field A -
192.168.1.0.......192.168.1.121.....192.168.1.15. ...192.168.2.12....192.168.2.20...192.168.3.50

How can I sort these by the 3rd and 4th octives?


My previous post left out some parts of the macro. This one should be
complete:

==================================
Option Explicit

Sub sortIP() 'sorts IP addresses
Dim i As Long, j As Long
Dim IP
Dim rg()

ReDim rg(Selection.Count - 1, 1)

For i = 0 To UBound(rg)
rg(i, 0) = Selection.Cells(i + 1, 1).Text
IP = Split(rg(i, 0), ".")
For j = 0 To 3
rg(i, 1) = rg(i, 1) & Right("0" & Hex(IP(j)), 2)
Next j
rg(i, 1) = "&H" & (rg(i, 1))
Next i

rg = BubbleSort(rg, 1)

For i = 0 To UBound(rg)
Selection.Cells(i + 1, 1) = rg(i, 0)
Next i

End Sub
Function BubbleSort(TempArray As Variant, d As Long) 'D is dimension to sort on
Dim Temp() As Variant
Dim i As Integer, j As Integer, k As Integer
Dim NoExchanges As Boolean

k = UBound(TempArray, 2)
ReDim Temp(0, k)

Do
NoExchanges = True

For i = 0 To UBound(TempArray) - 1
If TempArray(i, d) TempArray(i + 1, d) Then
NoExchanges = False
For j = 0 To k
Temp(0, j) = TempArray(i, j)
TempArray(i, j) = TempArray(i + 1, j)
TempArray(i + 1, j) = Temp(0, j)
Next j
End If
Next i
Loop While Not NoExchanges

BubbleSort = TempArray

End Function
==============================


--ron