Logo

Programming-Idioms

History of Idiom 100 > diff from v6 to v7

Edit summary for version 7 by :
[C]didn't read what had to be done.

Version 6

2015-12-27, 16:30:39

Version 7

2015-12-27, 16:31:24

Idiom #100 Sort by a comparator

Sort elements of array-like collection items, using a comparator c.

Idiom #100 Sort by a comparator

Sort elements of array-like collection items, using a comparator c.

Imports
#include <stdlib.h>
Imports
#include <stdlib.h>
Code
int comp(const void *a,const void *b)
{
	const int *ap=(const int *)a;
	const int *bp=(const int *)b;
	return *a-*b;
}

int main(void)
{
	int arr[]={1,6,3,7,2};
	qsort(arr,sizeof(arr)/sizeof(*arr),sizeof(*arr),comp);

	return 0;
}
Code
int c(const void *a,const void *b)
{
	const int *ap=(const int *)a;
	const int *bp=(const int *)b;
	return *a-*b;
}

int main(void)
{
	int arr[]={1,6,3,7,2};
	qsort(arr,sizeof(arr)/sizeof(*arr),sizeof(*arr),c);

	return 0;
}