473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 2412
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*****@DONTSPAMMEokstateDOT.edu> wrote in message
news:%2****************@TK2MSFTNGP11.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.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.
"Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> wrote in message
news:%2****************@TK2MSFTNGP11.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****@microsoft.com> wrote in message
news:u9**************@tk2msftngp13.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.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.
"Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> wrote in message
news:%2****************@TK2MSFTNGP11.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*****@DONTSPAMMEokstateDOT.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.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" <no****@microsoft.com> wrote in message
news:u9**************@tk2msftngp13.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.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.
"Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> wrote in message
news:%2****************@TK2MSFTNGP11.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.CreateElement(myqueryelement)
Dim nav As XPathNavigator = mdQuery.CreateNavigator();
Dim strXPathExpr As String = ' your xpath expression..
Dim exp As XPathExpression = nav.Compile(xtrXPathExpr)
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.public.xml
microsof.public.sharepoint.portalserver
microsoft.public.sharepoint.teamservices
microsof.public.sharepoint.portalserver.developmen t
microsoft.public.sharepoint.teamservices.caml
Sorry..hope that helps..
Imran.
"Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> wrote in message
news:O5**************@TK2MSFTNGP10.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.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" <no****@microsoft.com> wrote in message
news:u9**************@tk2msftngp13.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.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.
"Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> wrote in message
news:%2****************@TK2MSFTNGP11.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.XPath.XPathNavigator = ndQuery.CreateNavigator()
Dim strXPathExpr As String = "/Organizations/Organization_Row/OrgName"
Dim exp As System.Xml.XPath.XPathExpression = nav.Compile(strXPathExpr)
exp.AddSort(".", System.Xml.XPath.XmlSortOrder.Ascending,
System.Xml.XPath.XmlCaseOrder.None, "", Xml.XPath.XmlDataType.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****@microsoft.com> wrote in message
news:u1**************@TK2MSFTNGP12.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.CreateElement(myqueryelement)
Dim nav As XPathNavigator = mdQuery.CreateNavigator();
Dim strXPathExpr As String = ' your xpath expression..
Dim exp As XPathExpression = nav.Compile(xtrXPathExpr)
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.public.xml
microsof.public.sharepoint.portalserver
microsoft.public.sharepoint.teamservices
microsof.public.sharepoint.portalserver.developmen t
microsoft.public.sharepoint.teamservices.caml
Sorry..hope that helps..
Imran.
"Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> wrote in message
news:O5**************@TK2MSFTNGP10.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.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" <no****@microsoft.com> wrote in message
news:u9**************@tk2msftngp13.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.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.
"Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> wrote in message
news:%2****************@TK2MSFTNGP11.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*****@DONTSPAMMEokstateDOT.edu> wrote in message
news:ON**************@TK2MSFTNGP10.phx.gbl...
Thanks Imran, that is getting me in the right direction!
I tried this:

Dim nav As System.Xml.XPath.XPathNavigator = ndQuery.CreateNavigator()
Dim strXPathExpr As String = "/Organizations/Organization_Row/OrgName"
Dim exp As System.Xml.XPath.XPathExpression = nav.Compile(strXPathExpr)
exp.AddSort(".", System.Xml.XPath.XmlSortOrder.Ascending,
System.Xml.XPath.XmlCaseOrder.None, "", Xml.XPath.XmlDataType.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****@microsoft.com> wrote in message
news:u1**************@TK2MSFTNGP12.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.CreateElement(myqueryelement)
Dim nav As XPathNavigator = mdQuery.CreateNavigator();
Dim strXPathExpr As String = ' your xpath expression..
Dim exp As XPathExpression = nav.Compile(xtrXPathExpr)
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.public.xml
microsof.public.sharepoint.portalserver
microsoft.public.sharepoint.teamservices
microsof.public.sharepoint.portalserver.developmen t
microsoft.public.sharepoint.teamservices.caml
Sorry..hope that helps..
Imran.
"Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> wrote in message
news:O5**************@TK2MSFTNGP10.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.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" <no****@microsoft.com> wrote in message
news:u9**************@tk2msftngp13.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.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.
"Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> wrote in message
news:%2****************@TK2MSFTNGP11.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*****@DONTSPAMMEokstateDOT.edu> wrote in message
news:ON**************@TK2MSFTNGP10.phx.gbl...
Thanks Imran, that is getting me in the right direction!
I tried this:

Dim nav As System.Xml.XPath.XPathNavigator = ndQuery.CreateNavigator()
Dim strXPathExpr As String = "/Organizations/Organization_Row/OrgName"
Dim exp As System.Xml.XPath.XPathExpression = nav.Compile(strXPathExpr)
exp.AddSort(".", System.Xml.XPath.XmlSortOrder.Ascending,
System.Xml.XPath.XmlCaseOrder.None, "", Xml.XPath.XmlDataType.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****@microsoft.com> wrote in message
news:u1**************@TK2MSFTNGP12.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.CreateElement(myqueryelement)
Dim nav As XPathNavigator = mdQuery.CreateNavigator();
Dim strXPathExpr As String = ' your xpath expression..
Dim exp As XPathExpression = nav.Compile(xtrXPathExpr)
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.public.xml
microsof.public.sharepoint.portalserver
microsoft.public.sharepoint.teamservices
microsof.public.sharepoint.portalserver.developmen t
microsoft.public.sharepoint.teamservices.caml
Sorry..hope that helps..
Imran.
"Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> wrote in message
news:O5**************@TK2MSFTNGP10.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.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" <no****@microsoft.com> wrote in message
news:u9**************@tk2msftngp13.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.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.
"Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> wrote in message
news:%2****************@TK2MSFTNGP11.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
Imran,

And not one answer on my simple solution, even not tried, what is in my
opinion so simple to make with a XML document just by itterating through the
nodereader.

:-)

Cor
Nov 21 '05 #11
Imran,

And not one answer on my simple solution, even not tried, what is in my
opinion so simple to make with a XML document just by itterating through the
nodereader.

:-)

Cor
Nov 21 '05 #12
Cor, do you mean create my own sorter by iterating? It has been many moons
since I last did a bubble sort or anything like it in code. Do you have
idea?

Thanks!
Derek

"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Imran,

And not one answer on my simple solution, even not tried, what is in my
opinion so simple to make with a XML document just by itterating through
the nodereader.

:-)

Cor

Nov 21 '05 #13
Cor, do you mean create my own sorter by iterating? It has been many moons
since I last did a bubble sort or anything like it in code. Do you have
idea?

Thanks!
Derek

"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Imran,

And not one answer on my simple solution, even not tried, what is in my
opinion so simple to make with a XML document just by itterating through
the nodereader.

:-)

Cor

Nov 21 '05 #14
Derek,

When you see in this thread my message bellow than you see, as alternative
too the others that you can use a datatatable.

Because the structure looks often very much the same as an XMLdoc it is in
my opinion easy to fill using looping through an XML document using the
nodereader methode from the xmldocument.

Than a dataview on that table and use the sort property and all is done.

\\\\
Dim xmlString As String = "<department>" & _
"<employee name=""ABC"" age=""31"" sex=""male""/>" & _
"<employee name=""CDE"" age=""40"" sex=""male""/></department>"
Dim sr As New System.IO.StringReader(xmlString)
Dim doc As New Xml.XmlDocument
doc.Load(sr)
'or just in this case doc.LoadXML(xmlString)
Dim reader As New Xml.XmlNodeReader(doc)
While reader.Read()
Select Case reader.NodeType
Case Xml.XmlNodeType.Element
If reader.Name = "employee" Then
MessageBox.Show(reader.GetAttribute("name"))
End If
End Select
End While
///

I thought that I had once seen a methode that does it direct, however I lost
where it was.

However I think this can be done as well.

I hope it helps?

Cor

"Derek Martin"
Cor, do you mean create my own sorter by iterating? It has been many
moons since I last did a bubble sort or anything like it in code. Do you
have idea?

Thanks!
Derek

"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Imran,

And not one answer on my simple solution, even not tried, what is in my
opinion so simple to make with a XML document just by itterating through
the nodereader.

:-)

Cor


Nov 21 '05 #15
Derek,

When you see in this thread my message bellow than you see, as alternative
too the others that you can use a datatatable.

Because the structure looks often very much the same as an XMLdoc it is in
my opinion easy to fill using looping through an XML document using the
nodereader methode from the xmldocument.

Than a dataview on that table and use the sort property and all is done.

\\\\
Dim xmlString As String = "<department>" & _
"<employee name=""ABC"" age=""31"" sex=""male""/>" & _
"<employee name=""CDE"" age=""40"" sex=""male""/></department>"
Dim sr As New System.IO.StringReader(xmlString)
Dim doc As New Xml.XmlDocument
doc.Load(sr)
'or just in this case doc.LoadXML(xmlString)
Dim reader As New Xml.XmlNodeReader(doc)
While reader.Read()
Select Case reader.NodeType
Case Xml.XmlNodeType.Element
If reader.Name = "employee" Then
MessageBox.Show(reader.GetAttribute("name"))
End If
End Select
End While
///

I thought that I had once seen a methode that does it direct, however I lost
where it was.

However I think this can be done as well.

I hope it helps?

Cor

"Derek Martin"
Cor, do you mean create my own sorter by iterating? It has been many
moons since I last did a bubble sort or anything like it in code. Do you
have idea?

Thanks!
Derek

"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Imran,

And not one answer on my simple solution, even not tried, what is in my
opinion so simple to make with a XML document just by itterating through
the nodereader.

:-)

Cor


Nov 21 '05 #16
Derek,

I thought it was this, however I never used it

http://msdn.microsoft.com/library/de...taDocument.asp

Maybe it helps something?

Cor

"Derek Martin"
Cor, do you mean create my own sorter by iterating? It has been many
moons since I last did a bubble sort or anything like it in code. Do you
have idea?

Thanks!
Derek

"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Imran,

And not one answer on my simple solution, even not tried, what is in my
opinion so simple to make with a XML document just by itterating through
the nodereader.

:-)

Cor


Nov 21 '05 #17
Derek,

I thought it was this, however I never used it

http://msdn.microsoft.com/library/de...taDocument.asp

Maybe it helps something?

Cor

"Derek Martin"
Cor, do you mean create my own sorter by iterating? It has been many
moons since I last did a bubble sort or anything like it in code. Do you
have idea?

Thanks!
Derek

"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Imran,

And not one answer on my simple solution, even not tried, what is in my
opinion so simple to make with a XML document just by itterating through
the nodereader.

:-)

Cor


Nov 21 '05 #18
Hey Cor, thanks for your assistance, I also think I found an additional
solution - since I am pulling stuff out of SharePoint, I could in theory
query a view of a list instead of just the data results. Am trying that
now!

Derek

"Cor Ligthert" <no************@planet.nl> wrote in message
news:uM****************@TK2MSFTNGP09.phx.gbl...
Derek,

I thought it was this, however I never used it

http://msdn.microsoft.com/library/de...taDocument.asp

Maybe it helps something?

Cor

"Derek Martin"
Cor, do you mean create my own sorter by iterating? It has been many
moons since I last did a bubble sort or anything like it in code. Do you
have idea?

Thanks!
Derek

"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Imran,

And not one answer on my simple solution, even not tried, what is in my
opinion so simple to make with a XML document just by itterating through
the nodereader.

:-)

Cor



Nov 21 '05 #19
Hey Cor, thanks for your assistance, I also think I found an additional
solution - since I am pulling stuff out of SharePoint, I could in theory
query a view of a list instead of just the data results. Am trying that
now!

Derek

"Cor Ligthert" <no************@planet.nl> wrote in message
news:uM****************@TK2MSFTNGP09.phx.gbl...
Derek,

I thought it was this, however I never used it

http://msdn.microsoft.com/library/de...taDocument.asp

Maybe it helps something?

Cor

"Derek Martin"
Cor, do you mean create my own sorter by iterating? It has been many
moons since I last did a bubble sort or anything like it in code. Do you
have idea?

Thanks!
Derek

"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Imran,

And not one answer on my simple solution, even not tried, what is in my
opinion so simple to make with a XML document just by itterating through
the nodereader.

:-)

Cor



Nov 21 '05 #20

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

Similar topics

2
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...
10
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...
2
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...
20
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...
16
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...
48
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...
0
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,...
5
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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,...

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.