Sorting an array

I have a 2-dim array which holds values from 2 fields. How do I sort the arrary?

I have not seen any info on array sorts.

I’m new at LS so any help would be greatly appreciated.

Thanks!!!

Laura

Subject: sorting an array

I’m not sure if this is the best way, but here is a routine I’ve used that will sort a string array on all or a portion of the field value. You can modify it to handle a two dimensional array. For the initial call you will use the lower bound of the array for inLow and the upper bound for inHi. startpos is the first character of the string field on which to sort and endpos is the last. For instance to sort on the first 10 characters of the field use 1 and 10.

Sub QuickSortStringsAscending(sarray() As String, inLow As Long, inHi As Long, startpos As Integer, endpos As Integer)

'This will sort the elements in the string array by the values contained between startpos and endpos



Dim pivot As String

Dim tmpSwap As String

Dim tmpLow As Long

Dim tmpHi As Long



tmpLow = inLow

tmpHi = inHi



pivot = Mid$(sarray((inLow + inHi) / 2),startpos,endpos)



While (tmpLow <= tmpHi)

	

	While (Mid$(sarray(tmpLow),startpos,endpos) < pivot And tmpLow < inHi)

		tmpLow = tmpLow + 1

	Wend

	

	While (pivot < Mid$(sarray(tmpHi),startpos,endpos) And tmpHi > inLow)

		tmpHi = tmpHi - 1

	Wend

	

	If (tmpLow <= tmpHi) Then

		tmpSwap = sarray(tmpLow)

		sarray(tmpLow) = sarray(tmpHi)

		sarray(tmpHi) = tmpSwap

		tmpLow = tmpLow + 1

		tmpHi = tmpHi - 1

	End If

	

Wend



If (inLow < tmpHi) Then Call QuickSortStringsAscending(sarray(), inLow, tmpHi, startpos, endpos)

If (tmpLow < inHi) Then Call QuickSortStringsAscending(sarray(), tmpLow, inHi, startpos, endpos)	

End Sub

Subject: RE: sorting an array

That’s a good algorithm thanks for posting…it’s been awhile(college) since I wrote any sort algorithm like that you saved me alot of time!

Subject: RE: sorting an array

Just be aware that because QuickSort is recursive (it calls itself), you will run into stack problems on arrays of any real length.

Subject: @Sort, will this help?