Thanks Petr and John for the examples.
i don't know that we can add functions inside Type.. End Type . filling data in this way is concise and consumes less space and it looks neater.
Eros,
what do you think about possible new syntax:
In case of Primo's example, he could do for example:ARRAY SORT ArrayVariable([StartIndex]) [FOR nElements] [BY member][, COLLATE UCASE] [, {ASCEND | DESCEND}] [, ASFILES]
I checked the Core SDK, thanks to ability to thinBasic_GetTokenName, it is possible to get the UDT member name, and then, based on lookup to internal structures, do the magic.ARRAY SORT symbol BY SValue, DESCEND
Let me know, if okay, I would be happy to enjoy a little PB session working on this
Petr
Learn 3D graphics with ThinBASIC, learn TBGL!
Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB
Thanks Petr and John for the examples.
i don't know that we can add functions inside Type.. End Type . filling data in this way is concise and consumes less space and it looks neater.
Have you read the request, John, and what we are talking about here?
You always mention Script Basic, and I'm happy to compare different languages and how different languages have solved the same problem.
But at least remain on the subject: your link is just a generic array sort and not a sort of an UDT by one of the UDT elements.
www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000
I have to find a general way to sort any UDT by any UDT field.
But so far I didn't find a way but ... quite close.
In the meantime you can develop your own script function and adapt it to your needs like in the following example.
See template_QSort function and change as needed. You just need to change 4 lines if you need to sort by another UDT field.
Uses "console" Dim i As Long Type template ' -- Data sName As Long SValue As Word sCount As Long ' -- Functions ' Fills in all the values at once! function init(sName as long, sValue as word, sCount as long) me.sName = sName me.sValue = sValue me.sCount = sCount end function ' Represents type as string function to_string() as string return strformat$("{1}{2}{3}{4}{5}", me.sName, $TAB, me.sValue, $TAB, me.sCount) end function End Type function template_QSort(byref t1() as template, firstIndex as long, lastIndex as long) local low as long local high as long local pivotValue as dword '<---Same type as the UDT element to sort by local tTemp as template low = firstIndex high = lastIndex pivotValue = T1((firstIndex + lastIndex) / 2).sValue '---Change .sValue to any UDT element Repeat While T1(low).sValue < pivotValue '---Change .sValue to any UDT element low += 1 Wend While T1(high).sValue > pivotValue '---Change .sValue to any UDT element high -= 1 Wend If low <= high then '---SWAP tTemp = T1(low) T1(low) = T1(high) T1(high) = tTemp '--- low += 1 high -= 1 End If Until low > high If firstIndex < high then template_QSort(T1, firstIndex, high) EndIf If low < lastIndex then template_QSort(T1, low, lastIndex) End If End function Dim symbol(10) As template 'sName, sValue, sCount symbol(1).init( 22, 1, Rnd(1,1000)) symbol(2).init( 18, 2, Rnd(1,1000)) symbol(3).init( 2, 3, Rnd(1,1000)) symbol(4).init( 6, 4, Rnd(1,1000)) symbol(5).init( 9, 5, Rnd(1,1000)) symbol(6).init( 87, 6, Rnd(1,1000)) symbol(7).init( 54, 7, Rnd(1,1000)) symbol(8).init( 33, 8, Rnd(1,1000)) symbol(9).init( 4, 9, Rnd(1,1000)) symbol(10).init( 5, 10, Rnd(1,1000)) PrintL "============ before sorting" For i = 1 To 10 PrintL symbol(i).to_string() Next ' <- no need to specify variable with next Array Sort symbol, DESCEND 'ASCEND PrintL "============ after sorting" For i = 1 To 10 PrintL symbol(i).to_string() Next '[breakpoint] Quick Sort template_QSort(symbol, 1, ubound(symbol)) PrintL printl "After Quick Sort by .sValue" For i = 1 To 10 PrintL symbol(i).to_string() Next printl "end." WaitKey (60)
Last edited by ErosOlmi; 18-07-2018 at 15:42.
www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000
Hi Eros,You always mention Script Basic, and I'm happy to compare different languages and how different languages have solved the same problem.
But at least remain on the subject: your link is just a generic array sort and not a sort of an UDT by one of the UDT elements.
The only reason for the SB reference was to show how an array can be sorted using BASIC code rather than depending on the language.
I will try to be more respectful of this being the thinBasic forum.
Last edited by John Spikowski; 18-07-2018 at 16:18.
It is OK John.
Sorry but today I had a bad (working) day and I over reacted at first
Last edited by ErosOlmi; 18-07-2018 at 17:18.
www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000
Thanks Eros, impressive solution, i have replaced in template_QSort() the sValue by the third member sCount as you said, and it sorted the structured array using the third member
sorting3rd.PNG
for the users: you need the latest alpha release 1.10.5.0
www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000
My two cents for this:
I remember that i did such thing already to sort udts by their subelements when they were displayed in some table and the user clicked the header...
It was a bit of code because for speed reasons i used a separate sorting-function for every primitive type (byte, long, dword, single, double, string etc.) to sort.
Core-functions like UdtElement_Offset were my secret little helpers...
Anyway there was one thing that i had overseen and it caused me Errors: if the subelement to sort was an array itself, example foo(123).x(45)...
Dynamic subarrays were not implemented at this time but it would make a sense to sort them by countOf(foo(index).x) instead to complain an error.
Fixed subarrays i can not imagine how to sort - - - maybe peek$ and then sort it? Does it make sense?
Anyway, if using UI-module we could fill our data into some invisible listview-control and abuse its sorting-abilities - does it have them or do i confuse something here ? -
just have a column for every elements index...
I think there are missing some Forum-sections as beta-testing and support
Remember the Word Count Challange from the past?Anyway, if using UI-module we could fill our data into some invisible listview-control and abuse its sorting-abilities - does it have them or do i confuse something here ? -
just have a column for every elements index...
I used the OS sort command line utility to do my sorting of lists. Cheating but it worked great.
Welcome back to the forum!
Bookmarks