Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sorting a collection in VB Script

824 views
Skip to first unread message

janhuyzentruyt

unread,
Jul 22, 2004, 9:56:37 AM7/22/04
to
Does there exist a way to sort a collection alphabetically
in VB Script.

For example I want to make a list of the entities with some
extra info and output is to for example a .csv file via
VB-Script.

for each tab in model.tables
...
next

When using above loop, he gives the tables in a random (?)
order.

rickbell

unread,
Jul 22, 2004, 11:20:38 AM7/22/04
to
Jan,

Yes, the order will appear random without other
intervention.

If there is a way to do a sort in 1 or 2 steps I hope
someone posts that approach to the newsgroup. I have had to
use a few dozen lines of VBscript to actually sort the
objects.

Since I wanted the order of the objects permanently changed
I modified the Index property of the object’s collection;
e.g.: “Attribute = AttributeCollection.move(Index + 1,
Index)” to change the sequence of an attribute within an
entity. I can post an example if you request, but I got
most of the script from Vbscript newsgroups.

If your needs are temporary (and you don’t want to risk
accidentally saving the model) I suppose an array might be
the way to go? Or using PowerDesigner’s VBscript method
to create an ObjectCollection – but I haven’t done that
before.

Rick

Xiao Wang

unread,
Jul 22, 2004, 5:21:42 PM7/22/04
to
You could:
- create a quick sort function in VBScript (you can use google to search:
VBScript sorting)
- create an array to store the object names:
Dim tables()
ReDim tables(model.Tables.Count())
- add the table name or code in the array
- sort the array using the quick sort function
- for each item of the array
- get the table name or code
- find the table object
- export the data

You could use a Dictionary object to store the table name or code, the table
object to optimize the search of table by name.

Xiao

<Jan Huyzentruyt> wrote in message
news:40ffc888.12b...@sybase.com...

Marc

unread,
Jul 23, 2004, 4:13:03 AM7/23/04
to
Hi,

You may also sort the collection itself (The collection must be sortable, ie
of type "sorted collection", see vbscript chm in powerdesigner help).
Ex. (note the sort is absolutely not optimized -- nor really exempt of bug I
guess--, but it is not the issue there)

<<
SortCollection ActiveModel.Tables, "Name"
dim t
output "the sorted-by-Name list of tables in the model ->"
For each t in ActiveModel.Tables
output t.ShortDescription
next

sub SortCollection(pColl, sAttr)
if pColl.Count < 2 then exit sub ' don't sort a collection of one or less
element
if not (pColl.Item(0).HasAttribute(sAttr)) then exit sub ' check sAttr
attribute existence
dim i
for i = 0 to pColl.Count - 2
if (pColl.Item(i).GetAttributeText(sAttr) >
pColl.Item(i+1).GetAttributeText(sAttr)) then
output " -- need to invert " &
pColl.Item(i).GetAttributeText(sAttr) & " and " &
pColl.Item(i+1).GetAttributeText(sAttr)
pColl.Move i, i + 1
i = i - 1
end if
next
end sub
>>

Marc


"Xiao Wang" <xw...@sybase.com> wrote in message
news:410030db@forums-2-dub...

Marc

unread,
Jul 23, 2004, 4:18:04 AM7/23/04
to
Please change the line

<<
i = i - 1
>>
to
<<
if (i = 0) then i = i - 1 else i = i - 2
>>

Marc.

"Marc" <us...@host.com> wrote in message news:4100c983$1@forums-2-dub...

janhuyzentruyt

unread,
Jul 23, 2004, 4:19:16 AM7/23/04
to
Xiao,

Thank you very much.
It helps me a lot in what I wanted to do, except for one
thing :

what do you mean by "Dictionary object". I don't find not
much help on this in the documentation.

Jan

Xiao Wang

unread,
Jul 23, 2004, 5:49:20 AM7/23/04
to
Hi Jan,

Dictionary object is documented in the "Microsoft Windows Script" online
help.
You can download it from MS web site.
Dictionary objects uses a key and a value. In your case, the key is the
table code, the value is the table object.

Xiao

<Jan Huyzentruyt> wrote in message

news:4100caf7.1c3...@sybase.com...

0 new messages