Hi, Does anybody has handy a code that I could use to sort an array that contains dates, and the requirement is to sort this array from most current date on the top?
Thanks
Sunny Carrandi
Hi, Does anybody has handy a code that I could use to sort an array that contains dates, and the requirement is to sort this array from most current date on the top?
Thanks
Sunny Carrandi
Subject: Sort An array using Lotus Script
Found this in the Notes V5 forum. Hope it helps.
Also there’s code in the Public name & Address book that does this too. It’d be on the group form as there’s a button there to sort the members.
Subject: RE: Sort An array using Lotus Script
That’s a bubble sort, which gets really slow really fast as the array gets larger. A Shellsort is immensely faster:
(Yes, Quicksort can be faster still if the array is highly unsorted to begin with, but if it is nearly sorted already it’s slower and will run into stack overflow problems.)
Subject: Sort An array using Lotus Script
Here’s the code for a QuickSort, I’ve used this sort for years. It has not the disadvantage of performance issues when the array gets bigger and bigger. Enjoy it…
Option Public
'Employs a “divide-and-conquer” approach to get the data sorted. Works on
'subsets of data to sort, for each subset two pointers, i and j,
'are kept. In addition to this a sort-key is nominated (here, its the
'value at the left of the sublist being sorted). i and j are brought
'together
'(i.e., i increases, j decreases) swapping items into order as they
'approach. When i and j sense that the nominated value belongs where
'they’re sitting they move it there and then sort the data either side of
'the nominated value’s new point.
'PROS: Lightning quick, the Quicksort algorithm attempts to only compare
'and swap each element with another once.
'CONS: Although not very complicated, some structures are very difficult
'to code into a quicksort.
’ Pass the lower bound of the array to l, the upper bound of the array
'to r and the array to arrayelts.
'This is working on pass by reference once returned to the main program
'the array passed will be sorted. At that point you can save
'it to a document field.
’ Example using input of names you provide.
Sub QuickSort (l As Integer,r As Integer, arrayelts As Variant)
Dim m As Variant
Dim i As Integer
Dim t As Variant
Dim j As Integer
If r > l Then 'If there's nothing to sort, jump out
m = arrayelts(l) 'Initialize boundaries, nominate a value to sort
j = r
i = l
While (i < j) 'Repeat until i and j "meet in the middle"
While (arrayelts(i) <= m And i < r) 'Push in the boundaries while data is sorted
i = i + 1
Wend
While (arrayelts(j) > m)
j = j - 1
Wend
If i < j Then 'If there is data between i and j something is out of order - swap it
t = arrayelts(i)
arrayelts(i) = arrayelts(j)
arrayelts(j) = t
End If
Wend
t = arrayelts(l) 'Swap the nominated and bottom values - why we came here
arrayelts(l) = arrayelts(j)
arrayelts(j) = t
Call QuickSort (l, j - 1, arrayelts) 'Recurse and sort data either side of upper bound
Call QuickSort ((j + 1), r, arrayelts)
End If
End Sub
Subject: RE: Sort An array using Lotus Script
It does, however, have the disadvantage of running out of stack memory when handed a large-ish array that’s already sorted or nearly sorted. (The Lotusscript runtime does not provide much in the way of stack space.) Shell is, on average, nearly as fast as QS for arrays with integer bounds (LS doesn’t provide the ability to use larger-than-integer indices), isn’t recursive (so it can’t run out of stack space), and returns nearly instantly when the array is already sorted or nearly so (which is a “usual” case with Notes data).
There is also, lovingly preserved from the Ancient Days, the Domino Design Library from the old Sandbox, which contains another very fast, minimally-recursive sort routine (similar to merge sort). Most of the library has been superceded by built-in functions (both in formula language and in Lotusscript), but the sort routine is still among the best you can use in the Notes/Domino environment. You can find it at:
http://www.bananahome.com/ldd/sandbox.nsf/ByDate/816d64935ee8842485256c680051b592?OpenDocument
Subject: RE: Sort An array using Lotus Script
thanks so much Lourens,
Let me give a try, and I’ll let you know.
Thanks
Sunny ![]()