Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting ARRAY
#1
I have an array that has been sorted like this:
Code:
Copy      Help
arr[0]=|000||data000a||data000b|
arr[1]=|000||data001a||data001b|
arr[2]=|000||data002a||data002b|
arr[3]=|001||data003a||data003b|
arr[4]=|001||data004a||data004b|
arr[5]=|001||data005a||data005b|
arr[6]=|002||data006a||data006b|
arr[7]=|002||data007a||data007b|
arr[8]=|002||data008a||data008b|

I want to keep the initial sets ("|000|" "|001|" "|002|" etc) in order but randomize within those sets.
One result could look something like this:
Code:
Copy      Help
arr[2]=|000||data002a||data002b|
arr[1]=|000||data001a||data001b|
arr[0]=|000||data000a||data000b|
arr[4]=|001||data004a||data004b|
arr[3]=|001||data003a||data003b|
arr[5]=|001||data005a||data005b|
arr[7]=|002||data007a||data007b|
arr[8]=|002||data008a||data008b|
arr[6]=|002||data006a||data006b|

In the end I need an array populated with the random list.

I've written some code that does it, but it is ever so messy using RunTextAsFunction. I could post, but it's probably not worth it unless you want to see how it works to help better understand in case my explanation isn't clear.

I'm guessing that using the callback function for array.sort is the way to go, but I have no idea.

Thanks,
jimmy Vig
#2
Here's what I had...I cleaned it up a bit...like I said, fairly messy, but it does what I want:
Function SORT_DIRECTORY
Code:
Copy      Help
out
str SortedArray=
;|000||data000a||data000b|
;|002||data007a||data007b|
;|000||data001a||data001b|
;|002||data008a||data008b|
;|000||data002a||data002b|
;|001||data003a||data003b|
;|001||data004a||data004b|
;|001||data005a||data005b|
;|002||data006a||data006b|
;|000||data009a||data009b|
;|002||data010a||data010b|
_i=-1
str HOLD=""
str TEXT="str SETTEXT[]str FINALOUT"
ARRAY(str) arr = SortedArray
arr.sort
for(int'i 0 arr.len)
,ARRAY(str) arr2
,int i2 nt
,nt = tok(arr[i] arr2 -1 "||")
,for(i2 0 nt)
,,sel i2
,,,case 0
,,,if _i<val(arr2[i2])
,,,,if HOLD=""
,,,,,_s.format("str a%s=" arr2[i2])
,,,,else
,,,,,_s.format("ARRAY(str) ARR%s = a%s[]ARR%s.shuffle[]SETTEXT=ARR%s[]FINALOUT.addline(SETTEXT)[]str a%s=" HOLD HOLD HOLD HOLD arr2[i2])
,,,,TEXT.addline(_s)
,,,,_i=val(arr2[i2])
,,,,HOLD=(arr2[i2])
,,,,str ENDTEXT.format("ARRAY(str) ARR%s = a%s[]ARR%s.shuffle[]SETTEXT=ARR%s[]FINALOUT.addline(SETTEXT)" HOLD HOLD HOLD HOLD)
,,,_s.format(" |%s||%s||%s|" arr2[i2] arr2[i2+1] arr2[i2+2])err
,,,TEXT.addline(_s)
TEXT.addline(ENDTEXT)
str FINALs.format("ARRAY(str) FINALarr = FINALOUT[]for(int'FINALi 0 FINALarr.len)[][9]if FINALarr[FINALi]=''''[][9][9]err[][9][9]FINALarr.remove(FINALi)[][9][9]err[]out FINALarr")

TEXT.addline(FINALs)

RunTextAsFunction TEXT

I don't like the fact that I can't run this in an exe because of "RunTextAsFunction"
But it does do what I want it to do!

Thanks,
jimmy Vig
#3
Macro Macro807
Code:
Copy      Help
out
str SortedArray=
;|000||data000a||data000b|
;|002||data007a||data007b|
;|000||data001a||data001b|
;|002||data008a||data008b|
;|000||data002a||data002b|
;|001||data003a||data003b|
;|001||data004a||data004b|
;|001||data005a||data005b|
;|002||data006a||data006b|
;|000||data009a||data009b|
;|002||data010a||data010b|

ARRAY(str) arr=SortedArray
arr.shuffle
arr.sort(0 sort_dir_func 0)
out arr

Function sort_dir_func
Code:
Copy      Help
;/
function# param str&a str&b

;compares only first 4 characters
ret StrCompareN(a b 4)
#4
Awesome.

Shuffle the list and then compare the first like that. Slick.

Thank you so much!

I need to study much much much more.

Thanks,
jimmy Vig
#5
Just had to post again!

AMAZING! Thank you so much!


Forum Jump:


Users browsing this thread: 1 Guest(s)