custom sorting of lists ?
Steven Schveighoffer
schveiguy at gmail.com
Fri Oct 12 20:29:27 UTC 2018
On 10/12/18 3:40 PM, Codifies wrote:
> a while ago I wrote a doubly linked list (in C), which has a compare
> callback to allow custom sorting for example
>
> int cmpNodes(cnode_t* n1, cnode_t* n2)
> {
> mapNode_t* rn1 = (mapNode_t*)(n1->data);
> mapNode_t* rn2 = (mapNode_t*)(n2->data);
> if (rn1->G + rn1->H > rn2->G + rn2->H) return 1;
> return 0;
> }
>
> would be called by
>
> clistSort(openList, cmpNodes);
>
> The list would then be ordered by G + H fields (or any other algorithm
> you code)
>
>
> I notice there is a doubly linked list in the standard library, however
> it doesn't seem to allow for a custom compare, I'd rather not port my
> old C list code, can someone please give me some clues as to how I can
> reorder a list with a custom comparison...?
Unfortunately, I can't find a way to sort a doubly linked list in
phobos, so comparisons are somewhat moot.
However, if there *were* a sorting routine, generally the comparison
function is done via whatever type you give it, or is given a custom
comparison routine.
In my ancient dcollections library linked-list sorting is supported via
a `less` parameter:
https://github.com/schveiguy/dcollections/blob/82118cfd4faf3f1ad77df078d279f1b964f274e7/dcollections/LinkList.d#L997
-Steve
More information about the Digitalmars-d-learn
mailing list