From: Thomas Lerner on
Hello all!

I have a question about VB6:

I have an array of UDTs:

Public Type udtSpecial
Text As String
MyClass As Class1
End Type

Public m() As udtSpecial

I would like to sort this array by "Text".
Is this possible and if yes, how?

Thank you in advance for help.
Thomas
From: Karl E. Peterson on
Thomas Lerner formulated on Monday :
> Hello all!
>
> I have a question about VB6:
>
> I have an array of UDTs:
>
> Public Type udtSpecial
> Text As String
> MyClass As Class1
> End Type
>
> Public m() As udtSpecial
>
> I would like to sort this array by "Text".
> Is this possible and if yes, how?
>
> Thank you in advance for help.

Here ya go: http://www.lmgtfy.com/?q=vb+sort+udt

--
..NET: It's About Trust! http://vfred.mvps.org
Customer Hatred Knows No Bounds at MSFT
ClassicVB Users Regroup! comp.lang.basic.visual.misc
Free usenet access at http://www.eternal-september.org


From: ralph on
On Mon, 28 Jun 2010 18:55:17 +0200, Thomas Lerner
<t.lernernospam(a)hotmail.com> wrote:

>Hello all!
>
>I have a question about VB6:
>
>I have an array of UDTs:
>
>Public Type udtSpecial
> Text As String
> MyClass As Class1
>End Type
>
>Public m() As udtSpecial
>
>I would like to sort this array by "Text".
>Is this possible and if yes, how?
>

You can simply use any sort method (Bubble, etc.) and over-write the
comparison routine to use the string element.


From: Ulrich Korndoerfer on
Hi,

Thomas Lerner schrieb:
> Hello all!
>
> I have a question about VB6:
>
> I have an array of UDTs:
>
> Public Type udtSpecial
> Text As String
> MyClass As Class1
> End Type
>
> Public m() As udtSpecial
>
> I would like to sort this array by "Text".
> Is this possible and if yes, how?

For sorting one has to do comparisons and one has to change positions of
array items. So for sorting UDTs, just use any sorting algorithm you see
fit and:

- compare using eg. m(i).Text < m(i+1).Text
- change position (eg by swapping) using

Dim Temp As udtSpecial
Temp = m(i): m(i) = m(j): m(j) = Temp

--
Ulrich Korndoerfer

VB tips, helpers, solutions -> http://www.prosource.de/Downloads/
MS Newsgruppen Alternativen -> http://www.prosource.de/ms-ng-umzug.html
From: Jim Mack on
Thomas Lerner wrote:
> Hello all!
>
> I have a question about VB6:
>
> I have an array of UDTs:
>
> Public Type udtSpecial
> Text As String
> MyClass As Class1
> End Type
>
> Public m() As udtSpecial
>
> I would like to sort this array by "Text".
> Is this possible and if yes, how?

With (almost) any sort there are two basic operations: compare and
swap. In your case you'd make comparisons based on the Text field --
if m(1).Text > m(2).Text Then..., but swap the entire UDTs. Swapping
UDTs requires a separate Temp variable dimmed as the same type as the
array.

However, as a general rule you shouldn't swap the UDT elements
directly, but use a separate long-integer Index array.

First create a long-integer array with the same bounds as your UDT
array. Fill the array with its own index, so that Index(9) contains
the value "9" etc.

Then when comparing, if m(Index(1)).Text > m(Index(2)).Text Then...,
and when you need to swap, just swap the elements of the Index array,
not the UDT array.

It's faster, and as a bonus you can have several index arrays that
'sort' the UDT array on different criteria, and never change the UDT
array at all.

--
Jim Mack
Twisted tees at http://www.cafepress.com/2050inc
"We sew confusion"