Programming-Idioms

History of Idiom 28 > diff from v39 to v40

Edit summary for version 40 :
New C implementation by user [Dodopod]

Version 39

2017-04-29, 06:51:37

Version 40

2017-06-02, 15:59:29

Idiom #28 Sort by a property

Sort elements of array-like collection items in ascending order of x.p, where p is a field of the type Item of the objects in items.

Idiom #28 Sort by a property

Sort elements of array-like collection items in ascending order of x.p, where p is a field of the type Item of the objects in items.

Imports
#include <stdlib.h>
Code
int compareProp (const void *a, const void *b)
{
    return (*(const Item**)a)->p - (*(const Item**)b)->p;
}

qsort(items, N, sizeof(Item*), compareProp);
Comments bubble
items is an array of Item* with length N