473,406 Members | 2,217 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.

Sorting A Collection

I have a collection where the items in the collection are dates. I want to
iterate over the collection and build a value list string for the rowsource
of a listbox. The dates in the collection are not in chronological order. Is
there a way to first sort the collection and put the dates in chronological
order before creating the value list string? Or, how would I iterate over
the collection pulling out the dates in chronological order?

Thanks!

Scott
Nov 13 '05 #1
18 5711
On Thu, 16 Dec 2004 03:17:49 GMT, "Scott" <sm*****@nospam.please>
wrote:

One idea would be to use a disconnected ADO recordset to hold your
collection. You can create one out of thin air. They are sortable.

Another would be to use a sorting algorithm, e.g. bubblesort or qsort.

-Tom.

I have a collection where the items in the collection are dates. I want to
iterate over the collection and build a value list string for the rowsource
of a listbox. The dates in the collection are not in chronological order. Is
there a way to first sort the collection and put the dates in chronological
order before creating the value list string? Or, how would I iterate over
the collection pulling out the dates in chronological order?

Thanks!

Scott


Nov 13 '05 #2
On Wed, 15 Dec 2004 21:13:13 -0700, Tom van Stiphout <no*************@cox.net>
wrote:
On Thu, 16 Dec 2004 03:17:49 GMT, "Scott" <sm*****@nospam.please>
wrote:

One idea would be to use a disconnected ADO recordset to hold your
collection. You can create one out of thin air. They are sortable.
That was going to be my suggestion as well. I've used that, and works very
nicely. Technically, though, those are called Fabricated Recordsets, not
Disconnected Recordsets
Another would be to use a sorting algorithm, e.g. bubblesort or qsort.


I've done that, too. Use the ADO technique <g>.
Nov 13 '05 #3
Scott,

What is your "collection"? Is it not a table or a query? I would assume
it is, in which case all you need to do is make the listbox's rowsource
proprty a query (instead of a value list) to read from the table (or
existing query), and sort on date. No coding required.

HTH,
Nikos

Scott wrote:
I have a collection where the items in the collection are dates. I want to
iterate over the collection and build a value list string for the rowsource
of a listbox. The dates in the collection are not in chronological order. Is
there a way to first sort the collection and put the dates in chronological
order before creating the value list string? Or, how would I iterate over
the collection pulling out the dates in chronological order?

Thanks!

Scott

Nov 13 '05 #4
> I want to
iterate over the collection and build a value list string for the rowsource of a listbox.


could the BubbleSort method perhaps be used?
http://chrisrae.com/vba/routines/bubblesort.html

Nov 13 '05 #5
Jesper F wrote:
could the BubbleSort method perhaps be used?
http://chrisrae.com/vba/routines/bubblesort.html


Unless the array is relatively small, Bubblesort has the awful O(N^2)
running time. Do you want to create a Quicksort function too?

--
Bas Cost Budde, Holland
http://www.heuveltop.nl/BasCB/msac_index.html
I prefer human mail above automated so in my address
replace the queue with a tea
Nov 13 '05 #6
The collection will have ten items at most. How can I apply the bubblesort
routine to rearranging the dates in the collection to chronological order?

Thanks for staying with me on this!!

Scott
"Bas Cost Budde" <b.*********@heuvelqop.nl> wrote in message
news:cp**********@news2.solcon.nl...
Jesper F wrote:
could the BubbleSort method perhaps be used?
http://chrisrae.com/vba/routines/bubblesort.html


Unless the array is relatively small, Bubblesort has the awful O(N^2)
running time. Do you want to create a Quicksort function too?

--
Bas Cost Budde, Holland
http://www.heuveltop.nl/BasCB/msac_index.html
I prefer human mail above automated so in my address
replace the queue with a tea

Nov 13 '05 #7
Scott wrote:
The collection will have ten items at most. How can I apply the bubblesort
routine to rearranging the dates in the collection to chronological order?


One other question first: how do the dates get into this collection? If
you can do any form of insertion sort, that would save a lot of effort.

I can make a sorting function for a collection "tomorrow" (whatever that
means on this earth, taking time zones into consideration--mine is
GMT+1) but maybe you can play a bit:

Create a new collection
* loop the old collection, comparing every element to the new collection
* insert the element in the right place
Return the new collection (don't forget to clean up the old one!)

--
Bas Cost Budde, Holland
http://www.heuveltop.nl/BasCB/msac_index.html
I prefer human mail above automated so in my address
replace the queue with a tea
Nov 13 '05 #8
The application is for training courses and has a calendar that displays the
schedule of existing courses. I am working on a way to add new courses to
the schedule. A course may have multiple dates. The first step is that the
user clicks on any date on the calendar and that date is added to the
collection. The application can not rely on the user entering dates in
chronological order. Also a date may be deleted and another date put in its
place. So I need a way to sort the dates once they are all in the
collection. The bubblesort should work using MyCollection.Item(i). I prefer
not to use the second collection just to avoid the extra overhead.

Once the dates are in the collection on the proper order, the app will open
a data entry screen for entering the new course. The data entry form will
have a listbox that displays the dates in the collection and after the
course title and other data is entered, the user will click a button to add
the dates to a course subform.

Scott
"Bas Cost Budde" <b.*********@heuvelqop.nl> wrote in message
news:cp**********@news2.solcon.nl...
Scott wrote:
The collection will have ten items at most. How can I apply the bubblesort routine to rearranging the dates in the collection to chronological
order?
One other question first: how do the dates get into this collection? If
you can do any form of insertion sort, that would save a lot of effort.

I can make a sorting function for a collection "tomorrow" (whatever that
means on this earth, taking time zones into consideration--mine is
GMT+1) but maybe you can play a bit:

Create a new collection
* loop the old collection, comparing every element to the new collection
* insert the element in the right place
Return the new collection (don't forget to clean up the old one!)

--
Bas Cost Budde, Holland
http://www.heuveltop.nl/BasCB/msac_index.html
I prefer human mail above automated so in my address
replace the queue with a tea

Nov 13 '05 #9
Scott wrote:
I have a collection where the items in the collection are dates. I want to
iterate over the collection and build a value list string for the rowsource
of a listbox. The dates in the collection are not in chronological order. Is
there a way to first sort the collection and put the dates in chronological
order before creating the value list string? Or, how would I iterate over
the collection pulling out the dates in chronological order?

I just had a thought. The Add method has an optional Before
argument that you could use to add new items in a sorted
order.

Maybe you can adapt this to your needs:

Private colMySorted As New Collection

Public Sub AddSorted(lngValue)
Dim K As Long

If colMySorted.Count > 0 Then
For K = 1 To colMySorted.Count
If lngValue < colMySorted(K) Then
colMySorted.Add lngValue, , K
Exit Sub
End If
Next K
End If

colMySorted.Add lngValue

End Sub

--
Marsh
MVP [MS Access]
Nov 13 '05 #10
Marsh,

Thank you very much for responding!

Could you please explain the logic of your code.

Thanks,

Scott
"Marshall Barton" <ma*********@wowway.com> wrote in message
news:v4********************************@4ax.com...
Scott wrote:
I have a collection where the items in the collection are dates. I want toiterate over the collection and build a value list string for the rowsourceof a listbox. The dates in the collection are not in chronological order. Isthere a way to first sort the collection and put the dates in chronologicalorder before creating the value list string? Or, how would I iterate over
the collection pulling out the dates in chronological order?

I just had a thought. The Add method has an optional Before
argument that you could use to add new items in a sorted
order.

Maybe you can adapt this to your needs:

Private colMySorted As New Collection

Public Sub AddSorted(lngValue)
Dim K As Long

If colMySorted.Count > 0 Then
For K = 1 To colMySorted.Count
If lngValue < colMySorted(K) Then
colMySorted.Add lngValue, , K
Exit Sub
End If
Next K
End If

colMySorted.Add lngValue

End Sub

--
Marsh
MVP [MS Access]

Nov 13 '05 #11
Marsh,

I'm dense! Couldn't figure out where lngValue was coming from :(

This will work great by sorting as items are added. BTW, there should be
only one comma before K since we are using the Before parameter.

Thanks again,

Scott
"Marshall Barton" <ma*********@wowway.com> wrote in message
news:v4********************************@4ax.com...
Scott wrote:
I have a collection where the items in the collection are dates. I want toiterate over the collection and build a value list string for the rowsourceof a listbox. The dates in the collection are not in chronological order. Isthere a way to first sort the collection and put the dates in chronologicalorder before creating the value list string? Or, how would I iterate over
the collection pulling out the dates in chronological order?

I just had a thought. The Add method has an optional Before
argument that you could use to add new items in a sorted
order.

Maybe you can adapt this to your needs:

Private colMySorted As New Collection

Public Sub AddSorted(lngValue)
Dim K As Long

If colMySorted.Count > 0 Then
For K = 1 To colMySorted.Count
If lngValue < colMySorted(K) Then
colMySorted.Add lngValue, , K
Exit Sub
End If
Next K
End If

colMySorted.Add lngValue

End Sub

--
Marsh
MVP [MS Access]

Nov 13 '05 #12
As you've probably already figured out, the Before argument
is the THIRD argument of the Add method. The second
argument is the key argument, so you really do need two
commas (unless you are using a named argument).

If you are using the Key argument (my example didn't), of
course there would only be one comma between the Key and
Before arguments (the first comma is between the Item and
Key arguments).
--
Marsh
MVP [MS Access]

Scott wrote:
This will work great by sorting as items are added. BTW, there should be
only one comma before K since we are using the Before parameter.

Scott wrote:
>I have a collection where the items in the collection are dates. I wantto >iterate over the collection and build a value list string for therowsource >of a listbox. The dates in the collection are not in chronological order.Is >there a way to first sort the collection and put the dates inchronological >order before creating the value list string? Or, how would I iterate over
>the collection pulling out the dates in chronological order?


"Marshall Barton" wrote
I just had a thought. The Add method has an optional Before
argument that you could use to add new items in a sorted
order.

Maybe you can adapt this to your needs:

Private colMySorted As New Collection

Public Sub AddSorted(lngValue)
Dim K As Long

If colMySorted.Count > 0 Then
For K = 1 To colMySorted.Count
If lngValue < colMySorted(K) Then
colMySorted.Add lngValue, , K
Exit Sub
End If
Next K
End If

colMySorted.Add lngValue

End Sub

Nov 13 '05 #13
Scott wrote:
The application is for training courses and has a calendar that displays the
schedule of existing courses. I am working on a way to add new courses to
the schedule. A course may have multiple dates. The first step is that the
user clicks on any date on the calendar and that date is added to the
collection. The application can not rely on the user entering dates in
chronological order. Also a date may be deleted and another date put in its
place.
I think I can understand that :-).

I am a little surprised to see you use a Collection for this. A
(temporary) table would do perfectly, and then you don't have to worry
about sorting, the database will do that for you.
So I need a way to sort the dates once they are all in the
collection. The bubblesort should work using MyCollection.Item(i). I prefer
not to use the second collection just to avoid the extra overhead.
I am afraid the overhead in bubblesort is significantly larger, as you
have to swap items all the time.
Once the dates are in the collection on the proper order, the app will open
a data entry screen for entering the new course. The data entry form will
have a listbox that displays the dates in the collection and after the
course title and other data is entered, the user will click a button to add
the dates to a course subform.


The listbox can be based on the temp table. It makes the job easier, I
think, and less reliant on code hence less sensitive to maintenance.

--
Bas Cost Budde, Holland
http://www.heuveltop.nl/BasCB/msac_index.html
I prefer human mail above automated so in my address
replace the queue with a tea
Nov 13 '05 #14
Yes, you're right!

Thanks again for all your help!

Scott
"Marshall Barton" <ma*********@wowway.com> wrote in message
news:fv********************************@4ax.com...
As you've probably already figured out, the Before argument
is the THIRD argument of the Add method. The second
argument is the key argument, so you really do need two
commas (unless you are using a named argument).

If you are using the Key argument (my example didn't), of
course there would only be one comma between the Key and
Before arguments (the first comma is between the Item and
Key arguments).
--
Marsh
MVP [MS Access]

Scott wrote:
This will work great by sorting as items are added. BTW, there should be
only one comma before K since we are using the Before parameter.

Scott wrote:
>I have a collection where the items in the collection are dates. I want
to
>iterate over the collection and build a value list string for the

rowsource
>of a listbox. The dates in the collection are not in chronological
order.Is
>there a way to first sort the collection and put the dates in

chronological
>order before creating the value list string? Or, how would I iterate

over >the collection pulling out the dates in chronological order?

"Marshall Barton" wrote
I just had a thought. The Add method has an optional Before
argument that you could use to add new items in a sorted
order.

Maybe you can adapt this to your needs:

Private colMySorted As New Collection

Public Sub AddSorted(lngValue)
Dim K As Long

If colMySorted.Count > 0 Then
For K = 1 To colMySorted.Count
If lngValue < colMySorted(K) Then
colMySorted.Add lngValue, , K
Exit Sub
End If
Next K
End If

colMySorted.Add lngValue

End Sub

Nov 13 '05 #15
I decided to use a collection rather than a temp table out of concern that a
temp table would cause bloat of the database file. Numerous courses are
scheduled each month so there would be a lot of adding records and deleting
records to and from the temp table.

Thanks for all your help and hope you have a Merry Christmas and a Happy New
Year!

Scott
"Bas Cost Budde" <b.*********@heuvelqop.nl> wrote in message
news:cp**********@news2.solcon.nl...
Scott wrote:
The application is for training courses and has a calendar that displays the schedule of existing courses. I am working on a way to add new courses to the schedule. A course may have multiple dates. The first step is that the user clicks on any date on the calendar and that date is added to the
collection. The application can not rely on the user entering dates in
chronological order. Also a date may be deleted and another date put in its place.


I think I can understand that :-).

I am a little surprised to see you use a Collection for this. A
(temporary) table would do perfectly, and then you don't have to worry
about sorting, the database will do that for you.
So I need a way to sort the dates once they are all in the
collection. The bubblesort should work using MyCollection.Item(i). I prefer not to use the second collection just to avoid the extra overhead.


I am afraid the overhead in bubblesort is significantly larger, as you
have to swap items all the time.
Once the dates are in the collection on the proper order, the app will open a data entry screen for entering the new course. The data entry form will have a listbox that displays the dates in the collection and after the
course title and other data is entered, the user will click a button to add the dates to a course subform.


The listbox can be based on the temp table. It makes the job easier, I
think, and less reliant on code hence less sensitive to maintenance.

--
Bas Cost Budde, Holland
http://www.heuveltop.nl/BasCB/msac_index.html
I prefer human mail above automated so in my address
replace the queue with a tea

Nov 13 '05 #16
Scott wrote:
I decided to use a collection rather than a temp table out of concern that a
temp table would cause bloat of the database file. Numerous courses are
scheduled each month so there would be a lot of adding records and deleting
records to and from the temp table.


Nevertheless... if you do add these dates to a table in the end, why
sort them?

You could add the dates to the schedule table immediately, together with
a (bool?) indicator it's "on the add". When course data has been
completed, clear the flag; when the course is cancelled, delete the
flagged records. Or so.

If you do a lot of scheduling anyway, why bother with the database
bloat? You can compact it at regular intervals if you wish.

All this being said...

I tried to put a sorting procedure here but I hesitate. I suggest you
use an Array instead, and do sorted insertion. That is, when the user
clicks a date in the calendar, you insert it into the array:

* increase the array size with 1
* starting at the bottom (minus one), compare the date entered to the
date at the position
* if the new date is before this one, move the array date one down and
keep looping
* if the new date is *after* this one, write it in the position next to
the current (that contains a copy of the next-next position which you
have copied down in the step before) and stop looping

I think it is easy to wrap this in a Class with an Add method (taking a
date as parameter, which gets inserted in the correct position) and a
Store method (taking a table name and a field name as parameters,
basically running an INSERT query on the table filling the field with
the dates stored in the class).

Using a class this way keeps your code clear.
--
Bas Cost Budde, Holland
http://www.heuveltop.nl/BasCB/msac_index.html
I prefer human mail above automated so in my address
replace the queue with a tea
Nov 13 '05 #17
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

As an experiment, I once created a collection sorting routine for
Integers. Perhaps you can modify & use this to sort dates. The other
poster's recommendations are, perhaps, best for speed & maintenance, but
this was fun to code.

The class "CSortableIntegers" was a class I made so I could put integers
in a collection. It just had a public integer variable named "value."

Since collections only hold strings or objects are you storing the date
as a string value? If so it should be formatted YYYYMMDD so it sorts
correctly.

==== begin code ====

Sub CollectionIntShellSort(col As Collection)
' Purpose:
' Sort a collection of CSortableIntegers objects.
' Source:
' Translation of Shell sort from
' Data Management Techniques
' by John P. Grillo & J. D. Robertson
' Wm. C. Brown Company Publishers, Dubuque, IA, 1981, p.35
'
' Requirements:
' Collection item must have a readable "value" property,
' which is the sorting value.
' Note:
' Only works on 1 based arrays (collections).
' Think of items in the collection as a stack of plates:
' The top plate is the last item in the collection.
' If you pull a plate out of the middle the plate
' above it will come down & take it's place (index #).
' In:
' col The collection to sort
' Out:
' col Sorted
' Created:
' mgf 25apr99
' Modified:
'

Dim swapped As Boolean ' Swap flag
Dim i As Integer ' Right index
Dim j As Integer ' Left index
Dim m As Integer ' Mid-point index
Dim n As Integer ' Number of total items
Dim p As Integer ' Population count for current scan.

' Objects to swap.
Dim leftObj As Object
Dim rightObj As Object

n = col.Count
p = n
Do While p > 1
p = p \ 2
m = n - p
Do
swapped = False
For j = 1 To m
i = j + p
If col(j).value > col(i).value Then
' Save values & then swap their position in
collection.
Set leftObj = col(j)
Set rightObj = col(i)
col.Remove j
col.Add rightObj, , j 'Before:=j
col.Add leftObj, , i 'Before:=i
col.Remove i + 1
swapped = True
End If
Next j
Loop Until Not swapped
Loop

End Sub

==== end code ====

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQcMm3IechKqOuFEgEQJVFACdGhY5U5PPOBKLWkZ2eX+3kj rDWckAoP0y
j8+m0JMhX3H+SA7VyKQt/3wc
=Pv6b
-----END PGP SIGNATURE-----
Scott wrote:
The application is for training courses and has a calendar that displays the
schedule of existing courses. I am working on a way to add new courses to
the schedule. A course may have multiple dates. The first step is that the
user clicks on any date on the calendar and that date is added to the
collection. The application can not rely on the user entering dates in
chronological order. Also a date may be deleted and another date put in its
place. So I need a way to sort the dates once they are all in the
collection. The bubblesort should work using MyCollection.Item(i). I prefer
not to use the second collection just to avoid the extra overhead.

Once the dates are in the collection on the proper order, the app will open
a data entry screen for entering the new course. The data entry form will
have a listbox that displays the dates in the collection and after the
course title and other data is entered, the user will click a button to add
the dates to a course subform.

Scott
"Bas Cost Budde" <b.*********@heuvelqop.nl> wrote in message
news:cp**********@news2.solcon.nl...
Scott wrote:
The collection will have ten items at most. How can I apply the
bubblesort
routine to rearranging the dates in the collection to chronological


order?
One other question first: how do the dates get into this collection? If
you can do any form of insertion sort, that would save a lot of effort.

I can make a sorting function for a collection "tomorrow" (whatever that
means on this earth, taking time zones into consideration--mine is
GMT+1) but maybe you can play a bit:

Create a new collection
* loop the old collection, comparing every element to the new collection
* insert the element in the right place
Return the new collection (don't forget to clean up the old one!)

--
Bas Cost Budde, Holland
http://www.heuveltop.nl/BasCB/msac_index.html
I prefer human mail above automated so in my address
replace the queue with a tea

Nov 13 '05 #18
"Scott" <sm*****@nospam.please> wrote:
I decided to use a collection rather than a temp table out of concern that a
temp table would cause bloat of the database file.


See the TempTables.MDB page at my website which illustrates how to use a temporary
MDB in your app. http://www.granite.ab.ca/access/temptables.htm

Tony

--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Nov 13 '05 #19

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

Similar topics

7
by: Pete Davis | last post by:
A different question this time. I have a DataGrid bound to a collection. Is there any way for me to allow sorting? The DataGrid.AllowSorting=true doesn't work, but that's probably because it can't...
3
by: Ben Fidge | last post by:
Hi What are the options for sorting a StringCollection? I'm quite surprised that StringCollection doesn't have any sorting capability built in! Thanks Ben
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?
3
by: marc | last post by:
Hi, I have a collection of objects and i would like to add a flexibel sorting functionality I am looking for a colliection that gives my the following functionality a - have enumaration to...
6
by: Arthur Dent | last post by:
How do you sort a generic collection derived from System.Collections.ObjectModel.Collection? Thanks in advance, - Arthur Dent
10
by: Sjaakie | last post by:
Hi, I'm, what it turns out to be, fooling around with 3-tier design. At several websites people get really enthusiastic about using custom dataobjects instead of datasets/-tables. While trying to...
11
by: garyhoran | last post by:
Hi Guys, I have a collection that contains various attributes (stuff like strings, DateTime and Timespan) . I would like to access the collection in various orders at different points in the...
18
by: =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?= | last post by:
Hello. It seems a year is all it takes for one's proficiency in C++ to become too rusty. Professionally, I've been away from the language (and from programming in general), but I still preserve...
3
by: =?Utf-8?B?U2NvdHQ=?= | last post by:
Good afternoon, I would like to mimic the sorting capabilities in Excel where I can sort a list by up to four properties (columns in excel). I would like to pass a collection of objects to a...
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
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: 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:
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...
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
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...
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,...

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.