Sorting arrays of user defined data type 
Author Message
 Sorting arrays of user defined data type

I'm currently trying to sort an array of user defined data described
below.

Public Type TransRec
    Record_Type As String
    Account_Year_Ref As String
    Operator_ID As String
    Code As String
    Amount As String
    Date As String
    Time As String
    Srce As String
    Effective_Date As String
    Period_End_Date As String
    Cheque_Ind As String
    Sequence_No As String
End Type

The approximate size of the array is 20000 items and I would like to
sort on Account_Year_Ref and Sequence_No.

I'd be grateful for any pointers.

JM.



Tue, 17 Feb 2004 21:29:43 GMT  
 Sorting arrays of user defined data type

Quote:
> I'm currently trying to sort an array of user defined data described
> below.

> Public Type TransRec
>     Record_Type As String
>     Account_Year_Ref As String
>     Operator_ID As String
>     Code As String
>     Amount As String
>     Date As String
>     Time As String
>     Srce As String
>     Effective_Date As String
>     Period_End_Date As String
>     Cheque_Ind As String
>     Sequence_No As String
> End Type

> The approximate size of the array is 20000 items and I would like to
> sort on Account_Year_Ref and Sequence_No.

I'm not an authority on this, but where are you storing the data?
Don't most (all?) DB managers let you specify index fields?
Or are you wanting to load the data into an array and sort it?

Nelson



Wed, 18 Feb 2004 00:23:07 GMT  
 Sorting arrays of user defined data type
Assuming this is a Random Access file have a look at:

www.iss.u-net.com/dsort32.htm

Otherwise here is the Shell Sort algorithm.

' ##########################################
'
'
'
Sub USArSort(A$())
    Dim Max%, L9%, L8%, Gap%, S$

    Max = Val(A$(0))
    Gap = Int(Max / 2) + 1
    While Gap > 0
       For L9 = 1 To Max - Gap
           For L8 = L9 To 1 Step -Gap
               If A$(L8) > A$(L8 + Gap) Then
                  S$ = A$(L8)
                  A$(L8) = A$(L8 + Gap): A$(L8 + Gap) = S$
                  'SWAP A$(L8), A$(L8 + Gap)
                Else
                  L8 = 0
               End If
           Next
       Next
       Gap = Gap \ 2
    Wend
End Sub



Quote:
>I'm currently trying to sort an array of user defined data described
>below.

>Public Type TransRec
>    Record_Type As String
>    Account_Year_Ref As String
>    Operator_ID As String
>    Code As String
>    Amount As String
>    Date As String
>    Time As String
>    Srce As String
>    Effective_Date As String
>    Period_End_Date As String
>    Cheque_Ind As String
>    Sequence_No As String
>End Type

>The approximate size of the array is 20000 items and I would like to
>sort on Account_Year_Ref and Sequence_No.

>I'd be grateful for any pointers.

>JM.



Wed, 18 Feb 2004 16:01:06 GMT  
 Sorting arrays of user defined data type

Quote:

> I'm currently trying to sort an array of user defined data described
> below.

> Public Type TransRec
>     Record_Type As String
>     Account_Year_Ref As String
>     Operator_ID As String
>     Code As String
>     Amount As String
>     Date As String
>     Time As String
>     Srce As String
>     Effective_Date As String
>     Period_End_Date As String
>     Cheque_Ind As String
>     Sequence_No As String
> End Type
> The approximate size of the array is 20000 items and I would like to
> sort on Account_Year_Ref and Sequence_No.

20 000 strings in memory shouldn't be a big deal.  Instead of swapping
these structures around, how about swapping just "pointers" to them?
Also, will the array be "almost sorted" most of the time, perhaps with
new records added to the end and deleted records flagged with a special
value for Record_Type?  One algorithm does *not* fit all, or even most!

--
Joe Foster <mailto:jlfoster%40znet.com>  DC8s in Spaace: <http://www.xenu.net/>
WARNING: I cannot be held responsible for the above        They're   coming  to
because  my cats have  apparently  learned to type.        take me away, ha ha!



Thu, 19 Feb 2004 15:59:16 GMT  
 Sorting arrays of user defined data type

Quote:

> I'm currently trying to sort an array of user defined data described
> below.

I posted a code snippet on Planet Source Code some time back, that did
just this very thing. :-)

Click here to see it.

http://www.planet-source-code.com/xq/ASP/txtCodeId.12117/lngWId.1/qx/...

I have many intermediate level VB snippets at Planet Source Code,
search for the phrase "Kamilche" at Planet Source Code to see a list
of them.

--Kamilche



Fri, 20 Feb 2004 07:09:50 GMT  
 Sorting arrays of user defined data type

Quote:

> I'm currently trying to sort an array of user defined data described
> below.

Reposting because earlier message didn't 'take', forgive me if this is
a duplicate.

There is a code example that does exactly what you need at Planet
Source Code.
http://www.planet-source-code.com/xq/ASP/txtCodeId.12117/lngWId.1/qx/...

If the above link doesn't work, just go to
www.planet-source-code.com/vb and search for "Kamilche", you'll see a
number of intermediate-level VB programmer samples there.



Fri, 20 Feb 2004 09:41:59 GMT  
 Sorting arrays of user defined data type

Quote:
> A couple of minor nits:

> That's an awful lot of copying stuff around.  Perhaps you could
> construct a second array, with just indexes into the first array
> and perhaps copies of the keys.  That way, you need to copy much
> less baggage around before you know the final positions of all
> the elements in the original array.

Yep, that's what I'm using it for, though of course, this could sort any UDT.


Sat, 21 Feb 2004 09:38:33 GMT  
 Sorting arrays of user defined data type

MS Outlook Express 5.5 SP2 doesn't know to remove the left bracket in

Quote:
> > A couple of minor nits:

> > That's an awful lot of copying stuff around.  Perhaps you could
> > construct a second array, with just indexes into the first array
> > and perhaps copies of the keys.  That way, you need to copy much
> > less baggage around before you know the final positions of all
> > the elements in the original array.

> Yep, that's what I'm using it for, though of course, this could sort any UDT.

Then why aren't you loading the keys and an index within the original
array into an array of strings, getting "stability" nearly for free?
UCase$() any text, so you can use Option Compare Binary or StrComp()'s
vbBinaryCompare option.  Best of all, you can use this little routine:

Public Sub StrSwap(ByRef Str1 As String, ByRef Str2 As String)
#If Wussie Then
  Dim StrX As String
  StrX = Str1
  Str1 = Str2
  Str2 = StrX
#Else
  Dim PtrX As Long: PtrX = StrPtr(Str1)
  RtlMoveMemory ByVal VarPtr(Str1), ByVal VarPtr(Str2), 4&
  RtlMoveMemory ByVal VarPtr(Str2), PtrX, 4&
#End If
End Sub

Speaking of strings, the Fast String Concatenation sample at
<http://planet-source-code.com/xq/ASP/txtCodeId.10337/lngWId.1/qx/vb/s...>
is missing this:

  'Test linear concatenate speed
  ctr = 0
  All = vbNullString           ' start from scratch
  StartTime = timeGetTime
  'All = Space$(NumLoops * 10&) ' or make a guess
  For i = 1 To NumLoops
    If ctr + Len(s) > Len(All) Then
      All = All & s & Space$(Len(All)) ' \ 2&)
    Else
      Mid$(All, ctr + 1&) = s
    End If
    ctr = ctr + Len(s)
  Next
  All = Left$(All, ctr)
  Label3 = "Linear concatenation: " & (timeGetTime - StartTime) / 1000 _
    & " For " & Len(All) & " characters."

--
Joe Foster <mailto:jlfoster%40znet.com>  DC8s in Spaace: <http://www.xenu.net/>
WARNING: I cannot be held responsible for the above        They're   coming  to
because  my cats have  apparently  learned to type.        take me away, ha ha!



Sat, 21 Feb 2004 12:04:38 GMT  
 Sorting arrays of user defined data type

Quote:
>   'Test linear concatenate speed
>   ctr = 0
>   All = vbNullString           ' start from scratch
>   StartTime = timeGetTime
>   'All = Space$(NumLoops * 10&) ' or make a guess
>   For i = 1 To NumLoops
>     If ctr + Len(s) > Len(All) Then
>       All = All & s & Space$(Len(All)) ' \ 2&)

BUG!  The above should be more like this instead:

        All = Left$(All, ctr) & s & Space$(Len(All)) ' \2&

The performance hit appears to be negligible.

Quote:
>     Else
>       Mid$(All, ctr + 1&) = s
>     End If
>     ctr = ctr + Len(s)
>   Next
>   All = Left$(All, ctr)
>   Label3 = "Linear concatenation: " & (timeGetTime - StartTime) / 1000 _
>     & " For " & Len(All) & " characters."

--
Joe Foster <mailto:jlfoster%40znet.com>  Sign the Check! <http://www.xenu.net/>
WARNING: I cannot be held responsible for the above        They're   coming  to
because  my cats have  apparently  learned to type.        take me away, ha ha!


Sat, 21 Feb 2004 12:47:58 GMT  
 Sorting arrays of user defined data type

Quote:
> Then why aren't you loading the keys and an index within the original
> array into an array of strings, getting "stability" nearly for free?

Ew. You can't make me believe that copying my numeric-only keys into a
string, and copying them back again, on a list of half a million items
in memory, is FASTER than using the numerical values only! Put your
money where your mouth is - write an example that turns numeric values
into strings, sorts the string array, then converts them back to
numerics again, show me the timings compared to my sort routine, and
I'll be a true believer. :-)


Sun, 22 Feb 2004 04:02:18 GMT  
 Sorting arrays of user defined data type

Quote:


> > Then why aren't you loading the keys and an index within the original
> > array into an array of strings, getting "stability" nearly for free?

> Ew. You can't make me believe that copying my numeric-only keys into a
> string, and copying them back again, on a list of half a million items
> in memory, is FASTER than using the numerical values only! Put your
> money where your mouth is - write an example that turns numeric values
> into strings, sorts the string array, then converts them back to
> numerics again, show me the timings compared to my sort routine, and
> I'll be a true believer. :-)

Oddly enough, there's something funny going on at about the 200 000
mark:  I'm seeing quadratic behavior from string allocation, such
that creating 300 000 strings is taking twice as long as 200 000,
and 400 000 takes twice as long as 300 000.  Even though actually
sorting the strings is faster, at least in pcode, string allocation
kills me.  Of course, the same bug, and a bug it is, kills your
original example just as dead, thanks to that .Name member variable.

Well, well, well.  Your sort kicks my {*filter*}if the extra junk in your
UDT is removed and the whole mess is compiled to a native-code EXE.
Belief is overrated anyway.  =)

--
Joe Foster <mailto:jlfoster%40znet.com>  Sign the Check! < http://www.*-*-*.com/ ;
WARNING: I cannot be held responsible for the above        They're   coming  to
because  my cats have  apparently  learned to type.        take me away, ha ha!



Mon, 23 Feb 2004 11:29:33 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Sorting data stored in a user-define type array

2. Sorting and Comparing Elements in Arrays of User Defined Types

3. Passing Array or a user defined data type as a parameter to a storedprocedure in Oracle

4. Arrays in user defined data types?

5. Passing an array of user-defined data type to a class module

6. Q: variant data type with user defined data type

7. Passing Array or a user defined data type as a parameter to a storedprocedure in Oracle

8. Arrays in user defined data types?

9. Compile error: User-defined data type not defined!

10. Type mismatch on user defined data type

11. User Control - User-defined type not defined

12. VB4: Help: data-alignment in user defined data types

 

 
Powered by phpBB® Forum Software