July 5, 2005
Advanced Sorting with the Navigator Control
The Navigator Control has always supported any sort order you can dream up and implement in a .NET language. In these examples I use Visual Basic, but you could also use C#, J#, or any other language installed on your web site.
First, you will need to write a class that implements the IComparer interface to sort NavigatorItem’s. For example, this class sorts by category id:
Public Class CategoryIDSorter
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements System.Collections.IComparer.Compare
Dim item1 As StructuredSolutions.WebControls.NavigatorItem
Dim item2 As StructuredSolutions.WebControls.NavigatorItem
item1 = CType(x, StructuredSolutions.WebControls.NavigatorItem)
item2 = CType(y, StructuredSolutions.WebControls.NavigatorItem)
' No matter what comparison you make, always return these values
' 1 if item1 "is greater than" item2
' 0 if item1 "is equal to" item2
' -1 if item1 "is less than" item2
' This code compares the category id
If item1.CategoryID > item2.CategoryID Then
Return 1
ElseIf item1.CategoryID = item2.CategoryID Then
Return 0
Else
Return -1
End If
End Function
End Class
Next you need to supply one or two event handles. One for the TopLevelCreated event if you want to sort the top level categories, and one for the SubcategoriesCreated event if you want to sort subcategories. Each handler should sort the items using the sorting class you create. For example, these two handlers will both sort the items using the category id sorter shown above:
' Sorts the top level categories
Private Sub TopLevelCreated(ByVal sender As Object, _
ByVal e As StructuredSolutions.WebControls.NavigatorItemListEventArgs)
e.Items.Sort(New CategoryIDSorter)
End Sub
' Sorts sub-categories
Private Sub SubcategoriesCreated(ByVal sender As Object, _
ByVal e As StructuredSolutions.WebControls.NavigatorItemListEventArgs)
e.Items.Sort(New CategoryIDSorter)
End Sub
Finally, you need to tell the Navigator Control to use your event handlers by adding OnTopLevelCreated and/or OnSubcategoriesCreated attributes to the Navigator Control tag. For example, this opening tag will tell the Navigator Control to use the two handlers above:
<sfaddons:Navigator id="Navigator" runat="server" CssClass="navigator"
OnTopLevelCreated="TopLevelCreated"
OnSubcategoriesCreated="SubcategoriesCreated" >
You can sort by name, category id, phase of the moon, or any other criteria by creating a new class similar to CategoryIDSorter that implements your preferred order. I have attached a Navigator user control that includes the code shown above.
SortingNavigator.zip
To use SortingNavigator.ascx, follow these steps (assuming your have already installed the Navigator Control):
- Copy SortingNavigator.ascx to the Controls and ssl/Controls directories of your web site.
- Change the sorting code to meet your requirements.
- Edit LeftColumnNav.ascx and ssl/CommonControls/LeftColumnNav.ascx to change "TextNavigator1.ascx" to "SortingNavigator.ascx".
This site looks much better in a browser that supports current web standards, but it is accessible to any browser.
Download one now
Some parts of this site will not work effectively on this older browser.
Please consider
updating your browser