Search code examples
ckernighan-and-ritchie

K&R - Numerical sort of alpha characters?


What does it mean to numerically sort alpha characters in opposite of lexicographic, like in K&R 5-14 with option -n


Solution

  • In the second edition of K&R, section 5.11, the comparison function

    int numcmp(char *s1, char *s2); /* defined on page 121 */
    

    is used for numeric sorting (as opposed to using strcmp for lexicographic sorting). numcmp calls the function

    double atof(char s[]); /* defined on page 71 */
    

    which converts a string to its double-precision floating-point equivalent.

    In the example, sorting with the -n command line argument is done by the double values returned by the atof function.