Doug,
In addition to using a SortedList, you can create a custom Comparer object
(an object that implements IComparer) & pass it to the overloaded
ArrayList.Sort method.
http://msdn.microsoft.com/library/de...sorttopic2.asp
Something like:
Public Class AreaComparer
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object) As
Integer Implements System.Collections.IComparer.Compare
Dim row1 As DataRow = DirectCast(x, DataRow)
Dim row2 As DataRow = DirectCast(y, DataRow)
If String.Equals(row1!name, "All Areas") AndAlso
String.Equals(row2!name, "All Areas") Then
Return 0
ElseIf String.Equals(row1!name, "All Areas") Then
Return -1
ElseIf String.Equals(row2!name, "All Areas") Then
Return 1
Else
Return Comparer.DefaultInvariant.Compare(row1!name,
row2!name)
End If
End Function
End Class
Public Shared Sub Main()
Dim table As New DataTable("area")
table.Columns.Add("name", GetType(String))
table.Rows.Add(New Object() {"area 2"})
table.Rows.Add(New Object() {"area 1"})
table.Rows.Add(New Object() {"area 4"})
table.Rows.Add(New Object() {"area 3"})
table.Rows.Add(New Object() {" rea 5"})
Dim list As New ArrayList
list.AddRange(table.Rows)
Dim row As DataRow = table.NewRow()
row!name = "All Areas"
list.Add(row)
list.Sort(New AreaComparer)
For Each row In list
Debug.WriteLine(row!name, "area")
Next
End Sub
The above assumes there is only one row named "All Areas". AreaComparer
could be modified with a constructor that defines the value to look for &
column to look for (in which case I would use Object.Equals instead of
String.Equals so its "fully polymorphic".
Hope this helps
Jay
"Doug Bell" <Po*********@vodaphone.com.au> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
| Hi,
| I have a function that loads Rows from a field in a DataTable into an
| ArrayList.
| See below:
|
| It also adds a Row (eg "All Areas").
| I wanted this new row to be the first record so I added a preceeding space
| (eg " All Areas") and sorted the ArrayList.
|
| I have just found that there are some valid records that have one or more
| spaces so my added record is no longer first item in the list.
|
| Is there a way to create a custom sort for the ArrayList so that my added
| item is first in the list?
|
| Thanks
|
| Doug
|
|
|
|
|
| Private Function dacFillCombo(ByVal stTblNam As String, ByVal stDspFld As
| String, _
|
| ByVal stDsplVal As String) As ArrayList
|
| Dim dr As DataRow, al As New ArrayList
|
| Try
|
| al.Add(stDsplVal)
|
| For Each dr In dsFiles.Tables(stTblNam).Rows
|
| al.Add(dr(stDspFld).ToString)
|
| Next
|
| al.Sort()
|
| Return al
|
| Catch ex As Exception
|
| MsgBox(ex.ToString)
|
| End Try
|
| End Function
|
|