Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Alpha sorting an array?

Status
Not open for further replies.

zero2130

Programmer
Feb 2, 2001
1
US
How can I do this?

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?

Thanks
 
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 anybody miss perl hash tables yet?

JAC
 
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....

Thanx,
Ray
 
Why not load the array into a disconnected recordset and sort?
 
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....
 
Here is an example of using a recordset to sort.

option explicit

Dim myArray
Dim rst
Dim s
Dim xitem

myArray = Array("Parke Kuntz", "Jane Doe", "Mary Smith", "John Cole", "May Abbot","John Doy", "Mary Scale")

' 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

msgbox s

Hope this helps,

 
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.

Just my 2 cents.

-J
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top