[Implemented] Linked list sort functions for 3.80?

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

[Implemented] Linked list sort functions for 3.80?

Post by Psychophanta »

I would like to know if for PB 3.80 we will have any function to sort a linked list by one of its fields.

The matter is that I am implementing Sort-Extras by David McMinn (dave@blitz-2000.co.uk), which is very complete and versatile stuff, in my program, but if PB 3.80 will include it, then i prefer to wait.

Thanx !)

(See 'SortStructuredList()')
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

I would love to see native LinkedList sorting commands in PB especially to sort by structured variable offsets. Something like this:

SortList(LinkedList [, Offset, Direction])

:?:

Code: Select all

Structure TestStructure
    name.s
    date.s
    cost.s
    remain.s
EndStructure
NewList TestList.TestStructure()
...
;Add Values to all list members/offsets...
...
SortList(TestList(), name, 0)   ; Sort the list according to the 'name' offset in alphabetical order
SortList(TestList(), name, 1)   ; Sort the list according to the 'name' offset in reverse alphabetical order
:D
--Kale

Image
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Post by GPI »

I think, this variant would be easier to program:

Code: Select all

Structure TestStructure2
    name.s
    date.s
    cost.s
    remain.s
    Long.l
EndStructure
NewList TestList.TestStructure2()
...
;Add Values to all list members/offsets...
...
SortListString(TestList(),OffsetOf(TestStructure,name),1)
SortListLong(TestList(),OffsetOf(TestStructure,long),1)

newlist Test2.s()
SortListString(TestList(),0,1)
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

just added sorted linked list to my wish list... just ran into it...
while we're at it, swap for linked list and arrays?
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Post by freedimension »

GPI wrote:I think, this variant would be easier to program:

Code: Select all

Structure TestStructure2
    name.s
    date.s
    cost.s
    remain.s
    Long.l
EndStructure
NewList TestList.TestStructure2()
...
;Add Values to all list members/offsets...
...
SortListString(TestList(),OffsetOf(TestStructure,name),1)
SortListLong(TestList(),OffsetOf(TestStructure,long),1)

newlist Test2.s()
SortListString(TestList(),0,1)
Not a good Idea. What if you want to sort by two members, let's say first the name, than the date. Let's do it the right way from the beginning

Code: Select all

Structure TestStructure2
    name.s
    date.s
    cost.s
    remain.s
    Long.l
EndStructure

Procedure SortCompare(*element1, *element2)
;  Gibt 1 zurück wenn *element1 nach *element2 sortiert werden soll
;  -1 wenn's andersrum sein soll
; und 0 wenn die Einträge gleichwertig sind.
EndProcedure

NewList TestList.TestStructure2()
...
;Add Values to all list members/offsets...
...
ListSort(TestList(), @SortCompare() )
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

in a certain other language (that i am not going to mention :-)) there was a command to sort arrays

however, i don't think it has to be copied as such :-) but we might learn from it ;-)

before sorting, i'd prefer a SwapElement() command, which would allow easier implementation of other self built sorting algoritms (which i have no problem with by itself :-)):

Code: Select all

syntax: SwapElement( [ list() , ] n1.l , n2.l )

example:
SwapElement(testlist(),10,20)
SwapElement(10,20)

also, i would love to be able to address elements directly instead of going via SelectElement:

Code: Select all

SelectElement(10)
list() = whatever

list(10) = whatever

i would have no problem with a more simple sort command, that would sort the linked list based upon its contacts, without taking care of the fields... for simple lists that would do great, there is some influence over the way of sorting when defining your structs, it's fast to implement, and fancy stuff could always be done by hand

i guess most of us do mostly simple sorts, this would already be a great help

(no, i'm not going to mention something similar for arrays... oh, hi fred :-))

Code: Select all

SortListUp( [ list() ] )
SortListDown( [ list() ] )
how it's done elsewhere (reference only, disclaimer etc. etc. etc. :-))

Code: Select all

SSORT Function

Action:	sorts the elements in an array by its size using the Shell-Metzner algorithm.

Syntax:	SSORT x(s)[,n][,m%()]

SSORT x$(s) [WITH n()][OFFSET o][,n[,m%()]]

s:		+ or - for ascending or descending order
n,o:		iexp
x():		one dimensional array
x$():		one dimensional string array
n():		one dimensional integer array with 1,2 or 4 byte variables
m%():		one dimensional integer array with 4 byte variables

Explanation:	The s enclosed in round brackets can be replaced with a "+", a "-" or it may be left out. "+" or no 
specification results in arrays x() and m%() being sorted in ascending order. In this case, after the sorting, 
the smallest array element assumes the smallest index (0 for OPTION BASE 0 or 1 for OPTION BASE 1). 
"-" results in the array being sorted in descending order. In this case, after the sorting, the biggest array 
element assumes the smallest index.

The parameter n specifies that only the first n elements of the array should be sorted. For OPTION BASE 
0 these are the elements with indices 0 to "n"-1, and for OPTION BASE 1 the elements with indices 1 to 
"n". If n is given explicitly, it can be followed by a LONG integer array, which will be sorted together with 
the array x(), that is to say, each swap in array x() will also be performed in array m%(). This is particularly 
useful when the sorted array x() contains the sort key (for example the postal code), while other arrays 
contain the additional information whose information must maintain the same order as the keys.

When sorting string arrays (x$()) a sort criterion can be specified with WITH, in form of an array n() with at 
least 256 elements. If WITH is not given the normal ASCII table is used as the sort criterion.
Optionally, OFFSET o can specify the number of characters at the start of the string to ignore during 
sorting.

( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Re: Linked list sort functions for 3.80?

Post by tinman »

Psychophanta wrote:The matter is that I am implementing Sort-Extras by David McMinn (dave@blitz-2000.co.uk), which is very complete and versatile stuff, in my program, but if PB 3.80 will include it, then i prefer to wait.
Use Horst's linked list sorting stuff, it's much quicker than mine and works in pretty much the same way :)
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Thank you for advise, i will replace it now and check. :D
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

PB3.81 seems to include SortList() function, but seems to be not finished yet :( :!:
Post Reply