fork(1) download
  1. #include <stdio.h>
  2.  
  3. int compar(const void *pa, const void *pb)
  4. {
  5. size_t a = strlen(*(char**)pa);
  6. size_t b = strlen(*(char**)pb);
  7. if (a > b) return -1;
  8. if (a < b) return 1;
  9. return 0;
  10. }
  11.  
  12. int main(void)
  13. {
  14. char* a[] = {"1. hello world",
  15. "2. john doe at the bay watch",
  16. "3. Great Heavens",
  17. "4. altair",
  18. "5. Jim and Jamy"};
  19.  
  20. for (int i=0; i<5; ++i) puts(a[i]);
  21.  
  22. qsort(a, 5, sizeof a[0], compar);
  23.  
  24. puts("-----------------------------------");
  25.  
  26. for (int i=0; i<5; ++i) puts(a[i]);
  27.  
  28. return 0;
  29. }
  30.  
Success #stdin #stdout 0s 5472KB
stdin
Standard input is empty
stdout
1. hello world
2. john doe at the bay watch
3. Great Heavens
4. altair
5. Jim and Jamy
-----------------------------------
2. john doe at the bay watch
3. Great Heavens
5. Jim and Jamy
1. hello world
4. altair