(Solved) SortStructuredList with nested List

Just starting out? Need help? Post your questions and find answers here.
User avatar
chi
Addict
Addict
Posts: 1034
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

(Solved) SortStructuredList with nested List

Post by chi »

Is this a bug or am I just doing something wrong? :?

I'd assume that following code will sort List a() and also ALL nested Lists a()\b() ascending. But only the first nested List gets sorted...

Code: Select all

Structure SB
  name$
EndStructure

Structure SA
  name$
  List b.SB()
EndStructure

Global NewList a.SA()

RandomSeed(1)

; fill
For i=2 To 0 Step - 1  
  AddElement(a())
  a()\name$ = "Item " + Str(i)  
  For j=0 To 9    
    AddElement(a()\b())    
    a()\b()\name$ = Chr(Random(90, 65)) + Chr(Random(122, 97))   
  Next
Next

; sort
If 1
  ForEach a()  
    SortStructuredList(a(), #PB_Sort_Ascending, OffsetOf(SA\name$), TypeOf(SA\name$))   
    ForEach a()\b()    
      SortStructuredList(a()\b(), #PB_Sort_Ascending, OffsetOf(SB\name$), TypeOf(SB\name$))    
    Next  
  Next
EndIf

; debug
ForEach a()  
  Debug a()\name$  
  ForEach a()\b()    
    Debug Chr(9) + a()\b()\name$    
  Next  
Next
Last edited by chi on Sat Aug 03, 2019 6:57 pm, edited 1 time in total.
Et cetera is my worst enemy
User avatar
chi
Addict
Addict
Posts: 1034
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

Re: SortStructuredList with nested List

Post by chi »

Doesn't work with NextElement either...

Code: Select all

; sort
ResetList(a())
While NextElement(a())
  SortStructuredList(a(), #PB_Sort_Ascending, OffsetOf(SA\name$), TypeOf(SA\name$))
  ResetList(a()\b())
  While NextElement(a()\b())
    SortStructuredList(a()\b(), #PB_Sort_Ascending, OffsetOf(SB\name$), TypeOf(SB\name$))
  Wend
Wend
... but this works!

Code: Select all

; sort
SortStructuredList(a(), #PB_Sort_Ascending, OffsetOf(SA\name$), TypeOf(SA\name$))
SelectElement(a(), 0)
SortStructuredList(a()\b(), #PB_Sort_Ascending, OffsetOf(SB\name$), TypeOf(SB\name$))
SelectElement(a(), 1)
SortStructuredList(a()\b(), #PB_Sort_Ascending, OffsetOf(SB\name$), TypeOf(SB\name$))
SelectElement(a(), 2)
SortStructuredList(a()\b(), #PB_Sort_Ascending, OffsetOf(SB\name$), TypeOf(SB\name$))
Et cetera is my worst enemy
Sirius-2337
User
User
Posts: 53
Joined: Sat May 14, 2011 10:39 am

Re: SortStructuredList with nested List

Post by Sirius-2337 »

Sorting the list while 'foreaching' is not a good idea.

Code: Select all

  SortStructuredList(a(), #PB_Sort_Ascending, OffsetOf(SA\name$), TypeOf(SA\name$))  
  ForEach a()    
      SortStructuredList(a()\b(), #PB_Sort_Ascending, OffsetOf(SB\name$), TypeOf(SB\name$))   
  Next
User avatar
chi
Addict
Addict
Posts: 1034
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

Re: SortStructuredList with nested List

Post by chi »

yeah, of course :oops:
Et cetera is my worst enemy
User avatar
oreopa
Enthusiast
Enthusiast
Posts: 281
Joined: Sat Jun 24, 2006 3:29 am
Location: Edinburgh, Scotland.

Re: SortStructuredList with nested List

Post by oreopa »

If I understood correctly I don't think this qualifies as bug or otherwise... It seems logical to me that only the list that is specified is sorted, and not any further child/sublists. Perhaps the command could use a "#PB_Sort_Lists_Recursively"... but I don't think its really needed... YMMV :)
Proud supporter of PB! * Musician * C64/6502 Freak
hoerbie
Enthusiast
Enthusiast
Posts: 124
Joined: Fri Dec 06, 2013 11:57 am
Location: DE/BY/MUC

Re: SortStructuredList with nested List

Post by hoerbie »

Maybe I don't understand what you want, but I think, you simply don't need the first

Code: Select all

ForEach a()
for sorting the list a().

The list a() should only be sorted one time with

Code: Select all

SortStructuredList(a(), #PB_Sort_Ascending, OffsetOf(SA\name$), TypeOf(SA\name$))
and after this maybe with foreach a() you could loop over your lists a()\b() and sort them one by one?

Code: Select all

SortStructuredList(a(), #PB_Sort_Ascending, OffsetOf(SA\name$), TypeOf(SA\name$))   
ForEach a()    
  SortStructuredList(a()\b(), #PB_Sort_Ascending, OffsetOf(SB\name$), TypeOf(SB\name$))    
Next  
Post Reply