Sorting a delimited array

Results 1 to 3 of 3

Thread: Sorting a delimited array

  1. #1
    Ray K Guest

    Default 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. #2
    Join Date
    Dec 1969

    Default 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. #3
    Join Date
    Dec 1969
    Los Angeles, CA

    Default 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