Connecting Tech Pros Worldwide Help | Site Map

Sorting an array list containing objects?

Rob Meade
Guest
 
Posts: n/a
#1: Jan 10 '06
Dear all,

I have a class which contains an arraylist populated with other objects, for
example:

PrescriptionQueue - containing multiple instances of Prescription

I have the need on my web page to display this data which I have done,
however, now I would like to sort it based on a data item/direction selected
by the user from the web page.

I have separate data and display layers and was planning on doing my "sort"
in my display layer.

In order to do this I figured I would need to pull the Prescription objects
back out from my PrescriptionQueue, perhaps pop them into a DataTable/View
to make the sorting easier.

I then wondered whether it would be better to try and sort the
PrescriptionQueue itself.

Got a little lost after this point :o)

From what I've seen of the ArrayList.Sort you can do this quite easily:

ArrayList.Add("Red")
ArrayList.Add("Blue")
ArrayList.Add("Green")

ArrayList.Sort()

etc, but thats based on the value directly inside those elements of the
arraylist, is it possible to do something more like this:

For Each Prescription In PrsecriptionQueue

ArrayList.Add(Prescription)

Next

ArrayList.Sort(Prescription.PatientSurname)

I appreciate the above example is probably not syntactically correct, so
please excuse me, I am merely trying to give an example of what I would like
to achieve.

If anyone has any thoughts/suggestions as to doing this a better way I would
be most grateful - another idea I had was requesting the sort order when I
"get" my data in the first place.

Any help appreciated,

Regards

Rob
PS: Happy New Year!


Mantorok
Guest
 
Posts: n/a
#2: Jan 10 '06

re: Sorting an array list containing objects?


Look at the static method Array.Sort, you will have to break up your array
into 2 - one for keys and one for the corresponding values.

It's not particularly trivial but you can write your own method to deal with
it.

Kev

"Rob Meade" <ku.shn.tsews.thbu@edaem.bor> wrote in message
news:%23o$hBJfFGHA.1528@TK2MSFTNGP10.phx.gbl...[color=blue]
> Dear all,
>
> I have a class which contains an arraylist populated with other objects,
> for example:
>
> PrescriptionQueue - containing multiple instances of Prescription
>
> I have the need on my web page to display this data which I have done,
> however, now I would like to sort it based on a data item/direction
> selected by the user from the web page.
>
> I have separate data and display layers and was planning on doing my
> "sort" in my display layer.
>
> In order to do this I figured I would need to pull the Prescription
> objects back out from my PrescriptionQueue, perhaps pop them into a
> DataTable/View to make the sorting easier.
>
> I then wondered whether it would be better to try and sort the
> PrescriptionQueue itself.
>
> Got a little lost after this point :o)
>
> From what I've seen of the ArrayList.Sort you can do this quite easily:
>
> ArrayList.Add("Red")
> ArrayList.Add("Blue")
> ArrayList.Add("Green")
>
> ArrayList.Sort()
>
> etc, but thats based on the value directly inside those elements of the
> arraylist, is it possible to do something more like this:
>
> For Each Prescription In PrsecriptionQueue
>
> ArrayList.Add(Prescription)
>
> Next
>
> ArrayList.Sort(Prescription.PatientSurname)
>
> I appreciate the above example is probably not syntactically correct, so
> please excuse me, I am merely trying to give an example of what I would
> like to achieve.
>
> If anyone has any thoughts/suggestions as to doing this a better way I
> would be most grateful - another idea I had was requesting the sort order
> when I "get" my data in the first place.
>
> Any help appreciated,
>
> Regards
>
> Rob
> PS: Happy New Year!
>
>[/color]


Karl Seguin
Guest
 
Posts: n/a
#3: Jan 10 '06

re: Sorting an array list containing objects?


..Sort accepts an IComparer that tells it how to compare.

arr.Sort(ctype(new UserComparer(), IComparer))

public class UserComparer
implements IComparer

public function Compare(x as object, y as object) as integer implements
IComparer.Compare
return string.Compare(ctype(x, user).Name, ctype(y, user).Name)
end function

end class


Take a look at:
http://msdn.microsoft.com/library/de...sorttopic2.asp

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/


"Rob Meade" <ku.shn.tsews.thbu@edaem.bor> wrote in message
news:%23o$hBJfFGHA.1528@TK2MSFTNGP10.phx.gbl...[color=blue]
> Dear all,
>
> I have a class which contains an arraylist populated with other objects,
> for example:
>
> PrescriptionQueue - containing multiple instances of Prescription
>
> I have the need on my web page to display this data which I have done,
> however, now I would like to sort it based on a data item/direction
> selected by the user from the web page.
>
> I have separate data and display layers and was planning on doing my
> "sort" in my display layer.
>
> In order to do this I figured I would need to pull the Prescription
> objects back out from my PrescriptionQueue, perhaps pop them into a
> DataTable/View to make the sorting easier.
>
> I then wondered whether it would be better to try and sort the
> PrescriptionQueue itself.
>
> Got a little lost after this point :o)
>
> From what I've seen of the ArrayList.Sort you can do this quite easily:
>
> ArrayList.Add("Red")
> ArrayList.Add("Blue")
> ArrayList.Add("Green")
>
> ArrayList.Sort()
>
> etc, but thats based on the value directly inside those elements of the
> arraylist, is it possible to do something more like this:
>
> For Each Prescription In PrsecriptionQueue
>
> ArrayList.Add(Prescription)
>
> Next
>
> ArrayList.Sort(Prescription.PatientSurname)
>
> I appreciate the above example is probably not syntactically correct, so
> please excuse me, I am merely trying to give an example of what I would
> like to achieve.
>
> If anyone has any thoughts/suggestions as to doing this a better way I
> would be most grateful - another idea I had was requesting the sort order
> when I "get" my data in the first place.
>
> Any help appreciated,
>
> Regards
>
> Rob
> PS: Happy New Year!
>
>[/color]


Closed Thread