custom sorting of lists ?

Codifies a at b.com
Sat Oct 13 10:56:53 UTC 2018


On Saturday, 13 October 2018 at 07:48:04 UTC, Jacob Carlborg 
wrote:
> On 2018-10-12 21:40, 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...?
>
> I don't think you can sort a list because sorting requires 
> random access, which a list doesn't provide. Is there a reason 
> you cannot use an array?

it to port my old C pathfinding code, at the start of the path 
you don't know how long it will be and it need to grow and shrink 
depending on the obstacles and different potential paths it 
finds, using a list is just easier, I've ended up porting my C 
doubly linked list that has its own simple bubble sort...


More information about the Digitalmars-d-learn mailing list