Sorting a delimited array

# Thread: Sorting a delimited array

1. Ray K Guest

## Sorting a delimited array

Greetings . . .<BR><BR>I posted this question on the general forum, but didn&#039;t get an answer.<BR><BR>I have an array of delimited strings. For example, each array element has a string like "1~Application~Directory . . . (etc.)" (where the tilde [~] is my delimiter).<BR><BR>What I want to do is sort the array by the *second* (delimited) field. I looked at the FAQs for array sorting, and I assume that (by default) they sort on the first field, which is NOT what I want.<BR><BR>Is there an elegant and robust way of doing this without having to restructure each element in my array?<BR><BR>Thanks in advance for your assistance!

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## RE: Sorting a delimited array

You can easily do this. You just have to write the comparison code carefully.<BR><BR>Instead of doing something like<BR>&nbsp; &nbsp; IF ar(i) &#062; ar(i+1) THEN<BR>you&#039;ll have to write<BR>&nbsp; &nbsp; IF Split(ar(i),"~")(1) &#062; Split(ar(i+1),"~")(1) THEN<BR><BR>YUCK! Can you say SLOW, SLOW, SLOW!<BR><BR>The easiest way to modify this would be to convert your one-dimensional array of delimited strings into an ARRAY OF ARRAYS.<BR><BR>Thus:<BR><BR>Assume that "ar" is the name of the array as you now have it. So then you do:<BR><BR>&#060;%<BR>For i = 0 To UBound(ar)<BR>&nbsp; &nbsp; ar(i) = Split( ar(i), "~" )<BR>Next<BR>... now you do the sort, changing <BR>&nbsp; &nbsp; IF ar(i) &#062; ar(i+1) THEN<BR>... into this:<BR>&nbsp; &nbsp; IF ar(i)(1) &#062; ar(i+1)(1) THEN <BR>... which is a little slower than the original, but not much<BR>... and finish up with this *if* you need to:<BR>For i = 0 To UBound(ar)<BR>&nbsp; &nbsp; ar(i) = Join( ar(i), "~" )<BR>Next<BR>%&#062;<BR><BR>Of course, the better way would be to create an array of arrays in the first place, instead of using the delimited strings. But I&#039;ll assume you have some reason for them that isn&#039;t obvious.<BR><BR><BR><BR><BR>

3. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

## RE: Sorting a delimited array

Hey Billy, can yopu please look at my question bellow and add what ever comes to you miond....oomph..let me rephrase that...could you please add somethign with THAT regard.<BR><BR>thanks

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•