Hi Imran, I have been working on this and I think there might be an
easier way with my code and perhaps you can assist me!? The objects
begin life as an XML Document, retrieved from SharePoint:
Public Shared Function getorgs()
Dim orgslist As New ArrayList
...
sQuery.select = "/list[@id='" + global.Organizations +
"']"
myRequest.dsQuery = sQuery
Try
Dim myqueryelement As String '"Eq", "And", "Or"
Dim myquerystring As String 'The <field refs....>
'HERE IS MY QUERY, I'd like to add the sort, by field
OrgName ASC if possible
myqueryelement = "Eq"
myquerystring = "<FieldRef
Name='ActiveFlag'/><Value>1</Value>"
Dim ndQuery As XmlElement =
xmlDoc.CreateElement(myqueryelement)
ndQuery.InnerXml = myquerystring
spQuery.Where = ndQuery
myRequest.dsQuery.Query = spQuery
Dim myNode As XmlNode = myStsAd.Query(myRequest)
Dim xmlread As XmlDocument = New XmlDocument
Dim reader_node As XmlNode
reader_node =
myNode.SelectSingleNode("descendant::Organizations")
Dim orgidnodelist As XmlNodeList =
reader_node.SelectNodes("descendant::ID")
Dim orgnamenodelist As XmlNodeList =
reader_node.SelectNodes("descendant::OrgName")
...
'AT THIS POINT, I'd like to have the XMLDocument in
OrgName ASC order, so that as I loop through, I can add them to my
arraylist and it be in the proper order
counter = orgnamenodelist.Count
For i = 0 To counter - 1
'loop and create object
Dim organization As New
orgsobjects(orgnamenodelist(i)....)
...
'Add it
orgslist.Add(organization)
Next
'Return the object arraylist of objects, which are already
sorted, cause hopefully in the XPATH somewhere, I can sort the
XMLDocument BEFORE I start creating objects
Return orgslist
Catch ex As Exception
End Try
End Function
Basically, from my comments, I'd like to sort the document BEFORE making
the objects and loading them into the arraylist, saving me the
hassle...what do you think???
Thank you!
Derek
Imran Koradia said:
You'll have to have your own comparer that implements the IComparer
interface. Here's an example:
Public Class TestClass
Private mName As String
Public Property Name() As String
Get
Return mName
End Get
Set(ByVal Value As String)
mName = Value
End Set
End Property
End Class
Public Class TestClassSort
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object) As
Integer
Implements System.Collections.IComparer.Compare
Return String.Compare(DirectCast(x, TestClass).Name(),
DirectCast(y,
TestClass).Name(), CompareMethod.Binary)
End Function
End Class
Private Sub TestSort()
Dim arr As New ArrayList
Dim o(2) As TestClass
o(0) = New TestClass
o(0).Name = "One"
o(1) = New TestClass
o(1).Name = "Two"
o(2) = New TestClass
o(2).Name = "Three"
arr.AddRange(o)
For i As Integer = 0 To arr.Count - 1
Console.WriteLine(DirectCast(arr.Item(i), TestClass).Name)
Next
arr.Sort(New TestClassSort)
For i As Integer = 0 To arr.Count - 1
Console.WriteLine(DirectCast(arr.Item(i), TestClass).Name)
Next
End Sub
hope that helps..
Imran.
Hi there, I have been playing with sorting my arraylist and having some
troubles. Maybe just going about it wrong. My arraylist contains
objects
and one of the members of the object is 'name.' I would like to sort
the
arraylist based on object.name - is that possible?
Thanks!
Derek