How to sort a collection?

How to sort a collection?

Anonymous
Not applicable
463 Views
9 Replies
Message 1 of 10

How to sort a collection?

Anonymous
Not applicable
Hi,
This basic code works for arrays but I'm trying to modify it to work with
collection, any ideas what I'm doing wrong, can you not do
collection.item(i) = value?

'this code adapted from one of the many websites or ng posts, don't remember
which...

Public Sub ShellSortStringCollection(colStr As Collection)
Dim gappos As Integer
Dim swapped As Boolean
Dim tmp As Long
Dim tmpstr As String

gappos = colStr.Count \ 2
Do While gappos <> 0
Do
swapped = False
For tmp = 1 To colStr.Count - gappos
If (StrComp(colStr.Item(tmp + gappos), colStr.Item(tmp), vbTextCompare)
= -1) Then
tmpstr = colStr.Item(tmp + gappos)
'*********** here I get the error "Object required" ??? *****************
colStr.Item(tmp + gappos) = colStr.Item(tmp)
'***************************************************************************
*******
colStr.Item(tmp) = tmpstr
swapped = True
End If
Next tmp
Loop Until Not swapped
gappos = gappos \ 2
Loop
End Sub

I'm using 1 as the lbound of the collection, it seemed to bomb with 0 based.
Cant remember for sure But I think maybe collections are 1 based where
arrays are 0 based???
add watch on both of the sides of the experession show valid string data.
colStr.Item(tmp + gappos) = "some string"
colStr.Item(tmp) = "some other string"
so I dont' know what it means that it needs an object?

Any ideas?
Thanks
Mark
0 Likes
464 Views
9 Replies
Replies (9)
Message 2 of 10

Anonymous
Not applicable
Okay, I did it a weird way just to get it working.
Since I have subs that sort arrays i just copy the collection into an array,
sort the array, then put it back in the collection sorted.
I assume that's a stupid way to do it, despite the fact that it worked.
I assume the memory or processing power is wasted by creating an array as an
interim storage location and if it were a big collection, copying all that
data would maybe be slow, for me since it's a small string collection the
time is no issue but I'd still be interested in the *right* way to sort
collections, if there is one!?!

I've been studying veign's SimpleSort which he says is a method for a
collection class.
Since I'm not defining my own collection class I don't see how to apply this
in my situation.
Also it seems to be a function that expects a collection of class objects
not simple types like string or int etc. Since the argument list includes
strSortby which is looking at some kind of object property
apparently.(that's my best guess just by looking at the code) it looks like
it also handles the possibility of a "collection of collections of objects"
nested one deep.
I may be mis-understanding the code however, but I don't see what would
happen if it was nested deeper than once, ie "a collection of collections of
collections of objects" etc
Maybe I *should* be creating my own collection class tho, just dont' know if
that's recommended as best way to handle collections "generically,
modularly", ie applicable to reusable code in future (dll)etc.

My immediate usage was just to look at the files in a directory and since
dir returns them at random I wanted to sort them. This would obviously have
multiple usages through time and so should be part of my utility classes.
So I'm trying to write a function "GetFiles(directory, extension)". That
returns a collection of strings (filenames matching the extension). It
could have returned an array but since I don't know how many files will be
in a dir I'd have to either:
(1)redim recursively for ea file found (bad idea)
(2) redim once to some huge number(big enough to hold maximum number of
files ever encountered) then back again to actual size needed after array is
full(maybe slightly less bad idea but selection of "huge number" would be
subjective and you still have two redims).
(3) put in collection, get count, redim array once to size needed then put
in array and throw away collection (still seems like goofy idea but down to
just one redim and one wasted collection)
(4)some other idea I haven't thought of???

at this point I have one wasted array with one redim - so I don't know which
is "cheaper" one wasted collection or one wasted array since that seems to
be my choices at this point of my unknowingness!

My current question would be, is using a collection "inferior in any way" to
using an array to store the filenames in this kind of "generic" function?
and also I'd still be interested in anyones' idea of how to sort a
collection itself without resorting to the kludge I'm using currently (temp
array)

enjoy,
Maek
0 Likes
Message 3 of 10

Anonymous
Not applicable
I don't think collections are intended to be sorted! You got it to work by
sorting your array and adding the items sequentially. A collection doesn't
offer any method to allow insert of items into it in any particular position
to keep it sorted. If you added an item you would have to sort the whole
thing again. Collections are only intended for a convenient way to store
related items.
0 Likes
Message 4 of 10

Anonymous
Not applicable
Thanks ljb,
So in a situation like getting the filenames from a directory, would the
prefered way be to use array or collection?
Using a collection is easy to fill since you don't need to know in advance
what size you need.
But since one would almost always want the names sorted, then to sort the
collection requires a temp array.
Or you could use a temp colletion initially, size an array to it's .count
and then fill and sort the array.
or create a bigger array than you'd ever need and resize it after it's full,
then sort.
And ideas on the most efficient route?
Thanks
Mark

"ljb" wrote in message
news:EC1594E02BBB10E0602C74C536C6AD6D@in.WebX.maYIadrTaRb...
> I don't think collections are intended to be sorted! You got it to work by
0 Likes
Message 5 of 10

Anonymous
Not applicable
Assuming you're using some sort of loop, a pretty common way to do something
like this is to use a dynamic array and redim it every 100 items or so,
rather than redimming it every iteration of the loop. After you have built
your array run it through the sort function of your choice.

Collections are most commonly used to group objects (often similar objects),
arrays for everything else. Just a rule of thumb, there is no right and
wrong.


Regards,
Jacob Dinardi


"Mark Propst" wrote in message
news:31E8B9D8DD01FCB9A6221AA24F5BE811@in.WebX.maYIadrTaRb...
> Thanks ljb,
> So in a situation like getting the filenames from a directory, would the
> prefered way be to use array or collection?
> Using a collection is easy to fill since you don't need to know in advance
> what size you need.
> But since one would almost always want the names sorted, then to sort the
> collection requires a temp array.
> Or you could use a temp colletion initially, size an array to it's .count
> and then fill and sort the array.
> or create a bigger array than you'd ever need and resize it after it's
full,
> then sort.
> And ideas on the most efficient route?
> Thanks
> Mark
>
> "ljb" wrote in message
> news:EC1594E02BBB10E0602C74C536C6AD6D@in.WebX.maYIadrTaRb...
> > I don't think collections are intended to be sorted! You got it to work
by
>
>
>
0 Likes
Message 6 of 10

Anonymous
Not applicable
Thanks,
Redim it shall be.
0 Likes
Message 7 of 10

Anonymous
Not applicable
I use a lot of collection classes to manipulate my object classes. Most of them have a .Sort method and basically I do it similar to you. I haven't noticed any performance issues.
0 Likes
Message 8 of 10

Anonymous
Not applicable
Thanks for the response
so just to be sure I understand what you mean, does that mean your custom
object classes have properties and you then select one of those properties,
eg ".Weight" to be the sort criteria? Then you use a temp array to sort the
elements based on their property ".Weight"?

"CareyHatcher" wrote in message
news:f0edaaa.5@WebX.maYIadrTaRb...
> I use a lot of collection classes to manipulate my object classes. Most of
them have a .Sort method and basically I do it similar to you. I haven't
noticed any performance issues.
>
0 Likes
Message 9 of 10

Anonymous
Not applicable
Yes - I have a collection class to handle the object class with an .add method and a .Remove method. The steps I use in the sort method of the collection class are: 1. Define an array and a temporary collection 2. ReDim the array to the class Collection count 3. Fill the array with the property you want to sort 4. Sort 5. Outer loop thru the array and innner loop to compare the property values with the class collection, if a match then add to the temp collection and remove from the class collection. 6. Set the class collection to the temp collection. 7. set the temp collection = nothing
0 Likes
Message 10 of 10

Anonymous
Not applicable
Very cool.
Thank you.
0 Likes