[Implemented] Would be cool to have a SorStructuredtList()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

[Implemented] Would be cool to have a SorStructuredtList()

Post by BackupUser »

Restored from previous forum. Originally posted by redacid.

As the subject says: I´m working with a LinkedList. Now I need to have it sorted. If I had a command like "SortList" this would be much easier.

Look at this:

Structure adresses
Name.s
Street.s
City.s
EndStructure

NewList AdrList.adresses()

Now I have added several elements to this list and like to sort it:

SortList(AdrList,\Name) ; if I want it to be sorted according to the names.
SortList(AdrList,\Street); sort according to the city.


Of course i could write my own procedure to do this...but for this task I´m just too lazy

Oh, forgot to tell: I´m using the Windows-Version 3.20. An external Lib could be enough.

regards,
Redacid
---
Only Amiga makes it possible!

Edited by - redacid on 23 June 2002 15:09:38
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by redacid.

Hey, I just found a cool snippet on the resource-Site.... So thanx :)

But it would be much better to have it included in PB directly.

regards,
Redacid
---
Only Amiga makes it possible!
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.
Hey, I just found a cool snippet on the resource-Site.... So thanx :)

But it would be much better to have it included in PB directly.

regards,
Redacid
---
Only Amiga makes it possible!
While you're at it check out this forum thread also:
viewtopic.php?t=1363
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by horst.

I had some linked lists to sort recently, and I made two different
procedues: one to add elements to an already sorted list, and the
other for a complete (bubble) sort.

There are no structures (only simple strings) in my list, but I think
that can be easily modified.

; Add an element to (already) sorted list -----------------

NewList SortList.s()

Procedure AddToSortList(name.s)
ResetList(SortList())
Uname.s = UCase(name)
Repeat
If NextElement(SortList())
If Uname <= UCase(SortList())
InsertElement(SortList()) : done = 1
EndIf
Else
AddElement(SortList()) : done = 1
EndIf
Until done
SortList() = name
EndProcedure

; Sort linked list ------------------------------------

NewList MyList.s()

Procedure SortMyList()
n = CountList(MyList())
While n
FirstElement(MyList()) : s1.s = MyList()
n - 1
For i = 1 To n
NextElement(MyList())
s2.s = MyList()
If UCase(s2) < UCase(s1)
PreviousElement(MyList()) : MyList() = s2
NextElement(MyList()) : MyList() = s1
Else
s1 = s2
EndIf
Next
Wend
EndProcedure

; ----------------


Horst
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by redacid.

thanks guys!

regards,
Redacid
---
Only Amiga makes it possible!
Post Reply