473,398 Members | 2,427 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,398 software developers and data experts.

Sorting an array list containing objects?

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!
Jan 10 '06 #1
2 4707
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***************@edaem.bor> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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!

Jan 10 '06 #2
..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***************@edaem.bor> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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!

Jan 10 '06 #3

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

Similar topics

7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
1
by: aredo3604gif | last post by:
On Sun, 10 Apr 2005 19:46:32 GMT, aredo3604gif@yahoo.com wrote: >The user can dynamically enter and change the rule connection between >objects. The rule is a "<" and so given two objects: >a <...
7
by: Daniel | last post by:
does C# have any collection objects that support sort functionality so that I dont have to write my own sorting algorithm?
7
by: 00_ChInkPoIntD12 | last post by:
Can anyone confirm there isn't a Sort() method for WebControl Listbox in Asp.net? It is rather simple to write a method to do the sorting, but just wondering I shouldn't invent the wheel if...
0
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
25
by: Dan Stromberg | last post by:
Hi folks. Python appears to have a good sort method, but when sorting array elements that are very large, and hence have very expensive compares, is there some sort of already-available sort...
3
by: Greg D | last post by:
Hi all, I'm trying to sort an array of objects within an object. Included is a dumbed down example so that we can get at the meat of the issue without worrying about complexity or validation. ...
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
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...

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.