PxPlus User Forum

Main Board => Discussions => Wish List => Topic started by: James Zukowski on June 22, 2022, 05:05:19 PM

Title: Sort Array
Post by: James Zukowski on June 22, 2022, 05:05:19 PM
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: [Select]
New${all}=srt(Old${all}) ! String
New{all}=srt(Old{all}) ! Numeric

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!
Title: Re: Sort Array
Post by: keith.mcbride on June 22, 2022, 05:51:23 PM
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.
Title: Re: Sort Array
Post by: Loren Doornek on June 22, 2022, 06:50:36 PM
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: [Select]
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=",")
Title: Re: Sort Array
Post by: James Zukowski on June 23, 2022, 09:28:20 AM
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...)