Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Alpha sorting an array?

Alpha sorting an array?

Alpha sorting an array?

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?


RE: Alpha sorting an array?

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?


RE: Alpha sorting an array?

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


RE: Alpha sorting an array?

Why not load the array into a disconnected recordset and sort?

RE: Alpha sorting an array?

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

RE: Alpha sorting an array?

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
' insert the array into the recordset
For Each xitem in myArray
    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")," ")))

' choose the field to sort on
rst.Sort = "lname"

s = "sorted by last name"
Do until rst.EOF
    s = s & vbcrlf & rst.Fields("fullname")

msgbox s

s = "sorted by first name"
' 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")

msgbox s

Hope this helps,

RE: Alpha sorting an array?

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.


RE: Alpha sorting an array?

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close