Thread: C sort

  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Rep Power

    C sort

    if you have a text file that consists of columns of numbers, any ideas on how you could sort rows based on the numbers in one column. in other words if you sort the numbers in one column how can you get the rows to move with their associated number in the column you are sorting? Language: C.
  2. #2
  3. Headless Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Washington, USA
    Rep Power
    1. Load each line into a custom struct in memory. For example,
    c Code:
    struct _file_line {
    	int number;
    	char* column2;
    	char* column3;

    2. Write a function that can compare two structs and return which is less than the other.
    c Code:
    /* returns
     * <0 if a<b
     *  0 if a==b
     * >0 if a>b
    int compare(struct _file_line* a, struct _file_line* b) {
    	return a->number - b->number;

    3. Write a sorting function that sorts each struct, using that comparison function. (Or use a sorting function already available to you.)
    4. After the sorting is complete, your structs will be sorted. You can then write them back out into the file.

IMN logo majestic logo threadwatch logo seochat tools logo