I have an array that I want to be sorted alphabetically. Is there an easy way to do this or do you know of/have you written a function that'll do this?
There are a few traditional algorithms ( not that Al Gore has rhythm ) for performing such a sort, such as the bubble sort, etc. It would probably require slightly less code ( and would be a lot sexier ) to load the data into an XML Document object and then perform a select nodes with a sort applied.
Does Jac or anyone have an example of loading the data into an XML document? I think the sort part in XML will be easy but I've never yet loaded data from VBScript code into an XML document....
We've been wanting to start using XML here, so I thought it would be a good chance to get my feet wet. I'm using a bubble sort for now, but am intrigued by the recordset idea as well, since I've never heard of doing that....
' create a recordset
Set rst = createObject("ADODB.Recordset"
' create/append the fields to the recordset
rst.Fields.Append "fullname",129,50 'AD_Char = 129 and character length is 50
rst.Fields.Append "fname",129,50
rst.Fields.Append "lname",129,50
' open the recordset
rst.Open
' insert the array into the recordset
For Each xitem in myArray
rst.AddNew
rst.Fields("fullname" = xitem
' extract the first name from fullname
rst.Fields("fname" = Left(rst.Fields("fullname", Instr(rst.fields("fullname"," " -1)
' extract the last name from fullname
rst.Fields("lname" = Right(rst.Fields("fullname", (Len(rst.Fields("fullname")-Instr(rst.Fields("fullname"," "))
rst.Movenext
Next
rst.moveFirst
' choose the field to sort on
rst.Sort = "lname"
s = "sorted by last name"
Do until rst.EOF
s = s & vbcrlf & rst.Fields("fullname"
rst.MoveNext
Loop
msgbox s
s = "sorted by first name"
rst.MoveFirst
' choose the order of the fields you want to sort on
rst.Sort = "fname, lname"
Do until rst.EOF
s = s & vbcrlf & rst.Fields("fullname"
rst.MoveNext
Loop
Wow, not to be argumentative or disrespectful to anyone. But isn't both of those methods (XML and RS) a waste of server cycles?
I just don't understand why anyone would go to such lengths as to spawn new processes to sort an array in a production environment.
I would use the bubbleSort, XML and ADO have their places and IMHO, this isn't it.
Fo the XML method I am guessing you would have to actually create 2 instances of The MSXML parser. One for the DOM and one for the XSL transform that will actually perform the sort.
If all that was wanted was a simple sort, I would agree with you. With recordsets, one can sort on different columns by priority simply by filling in .sort = "field1, field2,field3..." This is the second sort. Doing this is more difficult using sorting routines.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.