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
[Implemented] Would be cool to have a SorStructuredtList()
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Pupil.
viewtopic.php?t=1363
While you're at it check out this forum thread also: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!
viewtopic.php?t=1363
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
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
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
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm