forked from DPrinceKumar/HacktoberFest2020-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cprogram-sorting
45 lines (35 loc) · 952 Bytes
/
Cprogram-sorting
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Defining comparator function as per the requirement
static int myCompare(const void* a, const void* b)
{
// setting up rules for comparison
return strcmp(*(const char**)a, *(const char**)b);
}
// Function to sort the array
void sort(const char* arr[], int n)
{
// calling qsort function to sort the array
// with the help of Comparator
qsort(arr, n, sizeof(const char*), myCompare);
}
int main()
{
// Get the array of names to be sorted
const char* arr[]
= { "geeksforgeeks", "geeksquiz", "clanguage" };
int n = sizeof(arr) / sizeof(arr[0]);
int i;
// Print the given names
printf("Given array is\n");
for (i = 0; i < n; i++)
printf("%d: %s \n", i, arr[i]);
// Sort the given names
sort(arr, n);
// Print the sorted names
printf("\nSorted array is\n");
for (i = 0; i < n; i++)
printf("%d: %s \n", i, arr[i]);
return 0;
}