Main Board > Wish List

Sort Array


James Zukowski:
Would it be possible to extend the capabilities of the SRT() function to include (1-dim) arrays? This would be useful when using dynamic arrays instead of *memory* files. Example:

--- Code: ---New${all}=srt(Old${all}) ! String
New{all}=srt(Old{all}) ! Numeric
--- End code ---

Optional parameter: Case-insensitive (1/0)
Very optional parameter: Dimension# to sort on (1-3)
Very very optional parameter: Reverse sequence (1/0)

Sorting 'in place' would be supported, e.g.: A${all}=srt(A${all})

Thanks for considering!

You can use the associative arrays (hash arrays) that are available:

100 dim a
200 a["03"]=3
300 a["01"]=1
400 a["02"]=2
500 for i$ index a{all}
600 print a[i$]
700 next i$

running gives

I think of the hash arrays as just a simplified memory file. Although they are implemented differently than a memory file.

Loren Doornek:
I've sorted string arrays using this logic.  The sort is all done in one line (line 40).  The rest of this is just setup for demo purposes.

--- Code: ---0010 BEGIN
0020 LET data$="Red,Orange,Yellow,Green,Blue,Indigo,Violet,"
0030 DIM a$[1:POS(","=data$,1,0)]; READ DATA FROM data$,SEP="," TO a${ALL}
0040 READ DATA FROM SRT(REC(CPL("iolist a${all}"))) TO a${ALL}
0050 PRINT "Raw Data:    ",data$
0060 PRINT "Sorted Data: ",REC(CPL("iolist a${all}"),SEP=",")

--- End code ---

James Zukowski:
One of the primary intents is to add entries to a list, sort the list, and then load that sorted list into a list_box. Whereas the SRT() function can be used on a string list, I believe having it available for arrays would enhance the functionality of the language.

I have yet to use associative arrays. I've found dynamic arrays to be extremely useful. I'm not sure how well they would work in the intended process.

That's a real slick method, and I'll probably take advantage of it. I just have to remember it.

Thanks, all!

(Still 'dreaming' of the SRT() extension...)


[0] Message Index

Go to full version