tags:

views:

27

answers:

1

How can I get the resulting generated list of links sorted out alphabetically according to "sTitle"? My sort function on line 272 is not giving me the results I need. Please help.

<script language="VB" runat="server">  

    Function sectionTitle(ByRef f As String)

        'Open a file for reading
        'Dim FILENAME As String = Server.MapPath("index.asp")
        Dim FILENAME As String = f

        'Get a StreamReader class that can be used to read the file
        Dim objStreamReader As StreamReader
        objStreamReader = File.OpenText(FILENAME)

        'Now, read the entire file into a string
        Dim contents As String = objStreamReader.ReadToEnd()

        'search string for <title>some words</title>     
        Dim resultText As Match = Regex.Match(contents, "(<title>(?<t>.*?)</title>)")
        'put result into new string
        Dim HtmlTitle As String = resultText.Groups("t").Value

        Return HtmlTitle

        ' If HtmlTitle <> "" Then
        'Response.Write(HtmlTitle)

        ' Else
        'Response.Write("<ul><li>b: " & contents & "</a></li></ul>")

       ' End If

    End Function


    Public Class linkItem

        Public myName As String
        Public myValue As String

        Public Sub New(ByVal myName As String, ByVal myValue As String)


            Me.myName = myName

            Me.myValue = myValue


        End Sub 'New 

    End Class 'linkItem



    Sub DirSearch(ByVal sDir As String)

        Dim d As String
        Dim f As String
        Dim mylist As New List(Of linkItem)


        Try
            For Each d In Directory.GetDirectories(sDir)
                'Response.Write("test c")

                For Each f In Directory.GetFiles("" & d & "", "index.asp")

                    'Response.Write("test a")                   
                    Dim sTitle As String = sectionTitle(f)
                    'remove wilbur wright college - from sTitle string
                    sTitle = Regex.Replace(sTitle, "My College - ", "")
                    'print section title - must come before search n replace string
                    f = Regex.Replace(f, "C:\\inetpub\\wwwroot\\mypath\\", "")
                    'add to list
                    mylist.Add(New linkItem(f, sTitle))
                    'print links as list
                    'Response.Write("<ul><li><a href='" & f & "'>" & sTitle & "</a></li></ul>")                 

                Next

        DirSearch(d)
            Next



        Catch excpt As System.Exception
    'Response.Write("test b")
            Response.Write(excpt.Message)
        End Try

        mylist.Sort(Function(p1, p2) p1.myValue.CompareTo(p2.myValue))

        mylist.ForEach(AddressOf ProcessLink)

    End Sub


    Sub ProcessLink(ByVal P As linkItem)
        If (True) Then
            Response.Write("<ul><li><a href='" & P.myName & "'>" & P.myValue & "</a></li></ul>")
        End If
    End Sub

</script>
<%     
    'Dim sDir As New DirectoryInfo(Server.MapPath(""))

    Call DirSearch((Server.MapPath("")))
%>
A: 

Check out the IComparable interface to help with this.

Basically, you need to teach your program what to use as a comparison point of reference for your class.

IComparable will allow you to make use of the CompareTo() method.

Here's the sample code if you're interested:

Public Class Temperature
    Implements IComparable

    Public Overloads Function CompareTo(ByVal obj As Object) As Integer _
        Implements IComparable.CompareTo

        If TypeOf obj Is Temperature Then
            Dim temp As Temperature = CType(obj, Temperature)

            Return m_value.CompareTo(temp.m_value)
        End If

        Throw New ArgumentException("object is not a Temperature")
    End Function

    ' The value holder
    Protected m_value As Integer

    Public Property Value() As Integer
        Get
            Return m_value
        End Get
        Set(ByVal Value As Integer)
            m_value = Value
        End Set
    End Property

    Public Property Celsius() As Integer
        Get
            Return (m_value - 32) / 2
        End Get
        Set(ByVal Value As Integer)
            m_value = Value * 2 + 32
        End Set
    End Property
End Class
Robert Greiner