473,837 Members | 1,438 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ArrayList.Sort Question

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
Nov 21 '05 #1
19 2468
You will need to implement the IComparable interface in the class statement
of the object(s) you have in the ArrayList.

Here is an article that shows how:

http://www.devx.com/dotnet/Article/21089
--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com

"Derek Martin" <dm*****@DONTSP AMMEokstateDOT. edu> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
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

Nov 21 '05 #2
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.Collecti ons.IComparer.C ompare
Return String.Compare( DirectCast(x, TestClass).Name (), DirectCast(y,
TestClass).Name (), CompareMethod.B inary)
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.WriteLi ne(DirectCast(a rr.Item(i), TestClass).Name )
Next
arr.Sort(New TestClassSort)
For i As Integer = 0 To arr.Count - 1
Console.WriteLi ne(DirectCast(a rr.Item(i), TestClass).Name )
Next
End Sub
hope that helps..
Imran.
"Derek Martin" <dm*****@DONTSP AMMEokstateDOT. edu> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
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

Nov 21 '05 #3
Thank you both! I will investigate :-)

Derek

"Imran Koradia" <no****@microso ft.com> wrote in message
news:u9******** ******@tk2msftn gp13.phx.gbl...
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.Collecti ons.IComparer.C ompare
Return String.Compare( DirectCast(x, TestClass).Name (),
DirectCast(y,
TestClass).Name (), CompareMethod.B inary)
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.WriteLi ne(DirectCast(a rr.Item(i), TestClass).Name )
Next
arr.Sort(New TestClassSort)
For i As Integer = 0 To arr.Count - 1
Console.WriteLi ne(DirectCast(a rr.Item(i), TestClass).Name )
Next
End Sub
hope that helps..
Imran.
"Derek Martin" <dm*****@DONTSP AMMEokstateDOT. edu> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
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


Nov 21 '05 #4
Derek,

As alternative to the others (not saying that it is better), can you look at
the datatable, that has a defaultview (dataview) build in to sort very.

Cor

"Derek Martin" <dm*****@DONTSP AMMEokstateDOT. edu>
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

Nov 21 '05 #5
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.Organiza tions + "']"

myRequest.dsQue ry = 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='ActiveFla g'/><Value>1</Value>"

Dim ndQuery As XmlElement =
xmlDoc.CreateEl ement(myqueryel ement)
ndQuery.InnerXm l = myquerystring
spQuery.Where = ndQuery
myRequest.dsQue ry.Query = spQuery
Dim myNode As XmlNode = myStsAd.Query(m yRequest)

Dim xmlread As XmlDocument = New XmlDocument
Dim reader_node As XmlNode
reader_node =
myNode.SelectSi ngleNode("desce ndant::Organiza tions")

Dim orgidnodelist As XmlNodeList =
reader_node.Sel ectNodes("desce ndant::ID")
Dim orgnamenodelist As XmlNodeList =
reader_node.Sel ectNodes("desce ndant::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(org namenodelist(i) ....)
...
'Add it
orgslist.Add(or ganization)
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" <no****@microso ft.com> wrote in message
news:u9******** ******@tk2msftn gp13.phx.gbl...
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.Collecti ons.IComparer.C ompare
Return String.Compare( DirectCast(x, TestClass).Name (),
DirectCast(y,
TestClass).Name (), CompareMethod.B inary)
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.WriteLi ne(DirectCast(a rr.Item(i), TestClass).Name )
Next
arr.Sort(New TestClassSort)
For i As Integer = 0 To arr.Count - 1
Console.WriteLi ne(DirectCast(a rr.Item(i), TestClass).Name )
Next
End Sub
hope that helps..
Imran.
"Derek Martin" <dm*****@DONTSP AMMEokstateDOT. edu> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
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


Nov 21 '05 #6
Derek,

Unfortunately, I'm not too familiar with SharePoint neither am I very well
versed with XML. But here's just an idea: Can you make use of the
XPathExpression via the XPathNavigator (which can be created off the
XmlElement you already have)? It has a AddSort method which I would think
should be what you are looking for. Something like this:

Dim mdQuery As XmlElement = xmlDoc.CreateEl ement(myqueryel ement)
Dim nav As XPathNavigator = mdQuery.CreateN avigator();
Dim strXPathExpr As String = ' your xpath expression..
Dim exp As XPathExpression = nav.Compile(xtr XPathExpr)
exp.AddSort(... 'sort by your field..

But again - I'm not sure if that would work. You would have better luck
trying in the XML/SharePoint groups:

microsoft.publi c.xml
microsof.public .sharepoint.por talserver
microsoft.publi c.sharepoint.te amservices
microsof.public .sharepoint.por talserver.devel opment
microsoft.publi c.sharepoint.te amservices.caml
Sorry..hope that helps..
Imran.
"Derek Martin" <dm*****@DONTSP AMMEokstateDOT. edu> wrote in message
news:O5******** ******@TK2MSFTN GP10.phx.gbl...
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.Organiza tions + "']"

myRequest.dsQue ry = 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='ActiveFla g'/><Value>1</Value>"

Dim ndQuery As XmlElement =
xmlDoc.CreateEl ement(myqueryel ement)
ndQuery.InnerXm l = myquerystring
spQuery.Where = ndQuery
myRequest.dsQue ry.Query = spQuery
Dim myNode As XmlNode = myStsAd.Query(m yRequest)

Dim xmlread As XmlDocument = New XmlDocument
Dim reader_node As XmlNode
reader_node =
myNode.SelectSi ngleNode("desce ndant::Organiza tions")

Dim orgidnodelist As XmlNodeList =
reader_node.Sel ectNodes("desce ndant::ID")
Dim orgnamenodelist As XmlNodeList =
reader_node.Sel ectNodes("desce ndant::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(org namenodelist(i) ....)
...
'Add it
orgslist.Add(or ganization)
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" <no****@microso ft.com> wrote in message
news:u9******** ******@tk2msftn gp13.phx.gbl...
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.Collecti ons.IComparer.C ompare
Return String.Compare( DirectCast(x, TestClass).Name (),
DirectCast(y,
TestClass).Name (), CompareMethod.B inary)
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.WriteLi ne(DirectCast(a rr.Item(i), TestClass).Name )
Next
arr.Sort(New TestClassSort)
For i As Integer = 0 To arr.Count - 1
Console.WriteLi ne(DirectCast(a rr.Item(i), TestClass).Name )
Next
End Sub
hope that helps..
Imran.
"Derek Martin" <dm*****@DONTSP AMMEokstateDOT. edu> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
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



Nov 21 '05 #7
Thanks Imran, that is getting me in the right direction!
I tried this:

Dim nav As System.Xml.XPat h.XPathNavigato r = ndQuery.CreateN avigator()
Dim strXPathExpr As String = "/Organizations/Organization_Ro w/OrgName"
Dim exp As System.Xml.XPat h.XPathExpressi on = nav.Compile(str XPathExpr)
exp.AddSort("." , System.Xml.XPat h.XmlSortOrder. Ascending,
System.Xml.XPat h.XmlCaseOrder. None, "", Xml.XPath.XmlDa taType.Text)

This gets the names in the correct order, but it doesn't reorder the
original document...if I could get it to do that, all would be perfect! Any
further ideas? If not, I am grateful for the help and I will try my luck in
public.xml
:-)

Cheers,
Derek

"Imran Koradia" <no****@microso ft.com> wrote in message
news:u1******** ******@TK2MSFTN GP12.phx.gbl...
Derek,

Unfortunately, I'm not too familiar with SharePoint neither am I very well
versed with XML. But here's just an idea: Can you make use of the
XPathExpression via the XPathNavigator (which can be created off the
XmlElement you already have)? It has a AddSort method which I would think
should be what you are looking for. Something like this:

Dim mdQuery As XmlElement = xmlDoc.CreateEl ement(myqueryel ement)
Dim nav As XPathNavigator = mdQuery.CreateN avigator();
Dim strXPathExpr As String = ' your xpath expression..
Dim exp As XPathExpression = nav.Compile(xtr XPathExpr)
exp.AddSort(... 'sort by your field..

But again - I'm not sure if that would work. You would have better luck
trying in the XML/SharePoint groups:

microsoft.publi c.xml
microsof.public .sharepoint.por talserver
microsoft.publi c.sharepoint.te amservices
microsof.public .sharepoint.por talserver.devel opment
microsoft.publi c.sharepoint.te amservices.caml
Sorry..hope that helps..
Imran.
"Derek Martin" <dm*****@DONTSP AMMEokstateDOT. edu> wrote in message
news:O5******** ******@TK2MSFTN GP10.phx.gbl...
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.Organiza tions +
"']"

myRequest.dsQue ry = 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='ActiveFla g'/><Value>1</Value>"

Dim ndQuery As XmlElement =
xmlDoc.CreateEl ement(myqueryel ement)
ndQuery.InnerXm l = myquerystring
spQuery.Where = ndQuery
myRequest.dsQue ry.Query = spQuery
Dim myNode As XmlNode = myStsAd.Query(m yRequest)

Dim xmlread As XmlDocument = New XmlDocument
Dim reader_node As XmlNode
reader_node =
myNode.SelectSi ngleNode("desce ndant::Organiza tions")

Dim orgidnodelist As XmlNodeList =
reader_node.Sel ectNodes("desce ndant::ID")
Dim orgnamenodelist As XmlNodeList =
reader_node.Sel ectNodes("desce ndant::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(org namenodelist(i) ....)
...
'Add it
orgslist.Add(or ganization)
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" <no****@microso ft.com> wrote in message
news:u9******** ******@tk2msftn gp13.phx.gbl...
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.Collecti ons.IComparer.C ompare
Return String.Compare( DirectCast(x, TestClass).Name (),
DirectCast(y,
TestClass).Name (), CompareMethod.B inary)
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.WriteLi ne(DirectCast(a rr.Item(i), TestClass).Name )
Next
arr.Sort(New TestClassSort)
For i As Integer = 0 To arr.Count - 1
Console.WriteLi ne(DirectCast(a rr.Item(i), TestClass).Name )
Next
End Sub
hope that helps..
Imran.
"Derek Martin" <dm*****@DONTSP AMMEokstateDOT. edu> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
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



Nov 21 '05 #8
Derek,

Apologies for the late reply. As I said, I'm not too familiar with XML
stuff. If you still haven't got that to work, I would suggest you ask this
in one of the xml groups. They should definitely help you solve this. Good
Luck !

Imran.
"Derek Martin" <dm*****@DONTSP AMMEokstateDOT. edu> wrote in message
news:ON******** ******@TK2MSFTN GP10.phx.gbl...
Thanks Imran, that is getting me in the right direction!
I tried this:

Dim nav As System.Xml.XPat h.XPathNavigato r = ndQuery.CreateN avigator()
Dim strXPathExpr As String = "/Organizations/Organization_Ro w/OrgName"
Dim exp As System.Xml.XPat h.XPathExpressi on = nav.Compile(str XPathExpr)
exp.AddSort("." , System.Xml.XPat h.XmlSortOrder. Ascending,
System.Xml.XPat h.XmlCaseOrder. None, "", Xml.XPath.XmlDa taType.Text)

This gets the names in the correct order, but it doesn't reorder the
original document...if I could get it to do that, all would be perfect! Any further ideas? If not, I am grateful for the help and I will try my luck in public.xml
:-)

Cheers,
Derek

"Imran Koradia" <no****@microso ft.com> wrote in message
news:u1******** ******@TK2MSFTN GP12.phx.gbl...
Derek,

Unfortunately, I'm not too familiar with SharePoint neither am I very well versed with XML. But here's just an idea: Can you make use of the
XPathExpression via the XPathNavigator (which can be created off the
XmlElement you already have)? It has a AddSort method which I would think should be what you are looking for. Something like this:

Dim mdQuery As XmlElement = xmlDoc.CreateEl ement(myqueryel ement)
Dim nav As XPathNavigator = mdQuery.CreateN avigator();
Dim strXPathExpr As String = ' your xpath expression..
Dim exp As XPathExpression = nav.Compile(xtr XPathExpr)
exp.AddSort(... 'sort by your field..

But again - I'm not sure if that would work. You would have better luck
trying in the XML/SharePoint groups:

microsoft.publi c.xml
microsof.public .sharepoint.por talserver
microsoft.publi c.sharepoint.te amservices
microsof.public .sharepoint.por talserver.devel opment
microsoft.publi c.sharepoint.te amservices.caml
Sorry..hope that helps..
Imran.
"Derek Martin" <dm*****@DONTSP AMMEokstateDOT. edu> wrote in message
news:O5******** ******@TK2MSFTN GP10.phx.gbl...
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.Organiza tions +
"']"

myRequest.dsQue ry = 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='ActiveFla g'/><Value>1</Value>"

Dim ndQuery As XmlElement =
xmlDoc.CreateEl ement(myqueryel ement)
ndQuery.InnerXm l = myquerystring
spQuery.Where = ndQuery
myRequest.dsQue ry.Query = spQuery
Dim myNode As XmlNode = myStsAd.Query(m yRequest)

Dim xmlread As XmlDocument = New XmlDocument
Dim reader_node As XmlNode
reader_node =
myNode.SelectSi ngleNode("desce ndant::Organiza tions")

Dim orgidnodelist As XmlNodeList =
reader_node.Sel ectNodes("desce ndant::ID")
Dim orgnamenodelist As XmlNodeList =
reader_node.Sel ectNodes("desce ndant::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(org namenodelist(i) ....)
...
'Add it
orgslist.Add(or ganization)
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" <no****@microso ft.com> wrote in message
news:u9******** ******@tk2msftn gp13.phx.gbl...
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.Collecti ons.IComparer.C ompare
Return String.Compare( DirectCast(x, TestClass).Name (),
DirectCast(y,
TestClass).Name (), CompareMethod.B inary)
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.WriteLi ne(DirectCast(a rr.Item(i), TestClass).Name )
Next
arr.Sort(New TestClassSort)
For i As Integer = 0 To arr.Count - 1
Console.WriteLi ne(DirectCast(a rr.Item(i), TestClass).Name )
Next
End Sub
hope that helps..
Imran.
"Derek Martin" <dm*****@DONTSP AMMEokstateDOT. edu> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
> 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
>
>



Nov 21 '05 #9
Derek,

Apologies for the late reply. As I said, I'm not too familiar with XML
stuff. If you still haven't got that to work, I would suggest you ask this
in one of the xml groups. They should definitely help you solve this. Good
Luck !

Imran.
"Derek Martin" <dm*****@DONTSP AMMEokstateDOT. edu> wrote in message
news:ON******** ******@TK2MSFTN GP10.phx.gbl...
Thanks Imran, that is getting me in the right direction!
I tried this:

Dim nav As System.Xml.XPat h.XPathNavigato r = ndQuery.CreateN avigator()
Dim strXPathExpr As String = "/Organizations/Organization_Ro w/OrgName"
Dim exp As System.Xml.XPat h.XPathExpressi on = nav.Compile(str XPathExpr)
exp.AddSort("." , System.Xml.XPat h.XmlSortOrder. Ascending,
System.Xml.XPat h.XmlCaseOrder. None, "", Xml.XPath.XmlDa taType.Text)

This gets the names in the correct order, but it doesn't reorder the
original document...if I could get it to do that, all would be perfect! Any further ideas? If not, I am grateful for the help and I will try my luck in public.xml
:-)

Cheers,
Derek

"Imran Koradia" <no****@microso ft.com> wrote in message
news:u1******** ******@TK2MSFTN GP12.phx.gbl...
Derek,

Unfortunately, I'm not too familiar with SharePoint neither am I very well versed with XML. But here's just an idea: Can you make use of the
XPathExpression via the XPathNavigator (which can be created off the
XmlElement you already have)? It has a AddSort method which I would think should be what you are looking for. Something like this:

Dim mdQuery As XmlElement = xmlDoc.CreateEl ement(myqueryel ement)
Dim nav As XPathNavigator = mdQuery.CreateN avigator();
Dim strXPathExpr As String = ' your xpath expression..
Dim exp As XPathExpression = nav.Compile(xtr XPathExpr)
exp.AddSort(... 'sort by your field..

But again - I'm not sure if that would work. You would have better luck
trying in the XML/SharePoint groups:

microsoft.publi c.xml
microsof.public .sharepoint.por talserver
microsoft.publi c.sharepoint.te amservices
microsof.public .sharepoint.por talserver.devel opment
microsoft.publi c.sharepoint.te amservices.caml
Sorry..hope that helps..
Imran.
"Derek Martin" <dm*****@DONTSP AMMEokstateDOT. edu> wrote in message
news:O5******** ******@TK2MSFTN GP10.phx.gbl...
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.Organiza tions +
"']"

myRequest.dsQue ry = 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='ActiveFla g'/><Value>1</Value>"

Dim ndQuery As XmlElement =
xmlDoc.CreateEl ement(myqueryel ement)
ndQuery.InnerXm l = myquerystring
spQuery.Where = ndQuery
myRequest.dsQue ry.Query = spQuery
Dim myNode As XmlNode = myStsAd.Query(m yRequest)

Dim xmlread As XmlDocument = New XmlDocument
Dim reader_node As XmlNode
reader_node =
myNode.SelectSi ngleNode("desce ndant::Organiza tions")

Dim orgidnodelist As XmlNodeList =
reader_node.Sel ectNodes("desce ndant::ID")
Dim orgnamenodelist As XmlNodeList =
reader_node.Sel ectNodes("desce ndant::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(org namenodelist(i) ....)
...
'Add it
orgslist.Add(or ganization)
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" <no****@microso ft.com> wrote in message
news:u9******** ******@tk2msftn gp13.phx.gbl...
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.Collecti ons.IComparer.C ompare
Return String.Compare( DirectCast(x, TestClass).Name (),
DirectCast(y,
TestClass).Name (), CompareMethod.B inary)
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.WriteLi ne(DirectCast(a rr.Item(i), TestClass).Name )
Next
arr.Sort(New TestClassSort)
For i As Integer = 0 To arr.Count - 1
Console.WriteLi ne(DirectCast(a rr.Item(i), TestClass).Name )
Next
End Sub
hope that helps..
Imran.
"Derek Martin" <dm*****@DONTSP AMMEokstateDOT. edu> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
> 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
>
>



Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3864
by: steve smith | last post by:
Hi I am new to this language, and still don't understand most of the concepts, I am trying to sort an arraylist by alphabet, I belive i need to use the IComparer interface, but have no idea how this works? Does anyone have a sample of how i can sort my arraylist of words. Also how is an arraylist iterated over and is it possible to obtain a count of the number of occurences of each owrd in the array list? Many thanks.
10
1530
by: C Downey | last post by:
Hello: I have an arraylist storing some very basic objects. The object is very basic, it has 2 properties : ID, and COUNT Before I add an object to the arraylist, I want to check if an object with that same ID already exists in the arraylist. If it does, I would like to increase the count of the matching object inside
2
5077
by: Rob G | last post by:
Hello, I have a basic understanding of Objects, inheritance, polymorhpism, etc. I am new to VB.NET and I am having a problem figuring this out. I want to sort a CheckBoxList. Using the ArrayList seemed to make the most sense. When I use it I get the error: At least one object must implement IComparable. Here's the code that produces that error: Public Class Sorting
20
5990
by: Dennis | last post by:
I use the following code for a strongly typed arraylist and it works great. However, I was wondering if this is the proper way to do it. I realize that if I want to implement sorting of the arraylist then I have to handle this with a sort method that uses comparer. I can reference the properties of the Arraylist directly such as dim mylist as new FrameList mylist.Add(new FrameStructure) mylist(0).first = "blabla..." mylist(0).second...
16
6435
by: RCS | last post by:
So I have an ArrayList that gets populated with objects like: myAL.Add(new CustomObject(parm1,parm2)); I'm consuming this ArrayList from an ObjectDataSource and would like to have this support sorting (because it's ultimately being consumed in a GridView). I can't simply sort the ArrayList (because it only knows it's holding a bunch of objects). So I need a way to sort the ArrayList, based on the data - that is within the objects that...
48
4498
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a lot faster with both methods using .NET 1.1, that's why I am pretty sure its a (rather serious) bug. Below you can find C# test case that should allow you to reproduce this error, to run it you will need to put 2 data files into current directory...
0
1976
by: DarkBlaZeR | last post by:
Hi all, This steps I made: 1. Read a file in 2. Put each line of the file in an arraylist 3. Make a collection of the arraylist so you can sort them 4. Search for a word Now my problem is, the text file will be something like this:
5
1283
by: Tony | last post by:
Hello! I have a class called Item as follows. I use CompareTo to be able to sort an ArrayList containg Items on heatNumber. This works fine. Now to my question. I also want to be able to sort a different ArrayList containg Items on steelGrade. How is that done because I can only have one CompareTo for the Item class. class Item : IComparer {
0
9842
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9682
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10874
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10566
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10623
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9398
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7806
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5668
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5848
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.