Thread: Sorting Alphabetically

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    1

    Sorting Alphabetically

    Ok basically what I need to do is print out an array in alphabetical order. However my array is a struct type:

    Code:
    #include<stdio.h>
    //GLOBAL DECLERATIONS
    struct location{
    	int east,north;
    	char name[20],place[20];
    };
    struct location site[20];
    int nsites=0;
    
    //LIST SITE CODE
    int list_site(int index)
    {
    	if(index<0 || index>nsites-1)
    		return -1;
    	printf("site[%d]: %d %d %s %s\n",index,site[index].east,site[index].north,site[index].name,site[index].place);
    	return 0;
    }
    
    //LIST ALL SITES CODE
    void list_sites(void)
    {
    	int i;
    	for(i=0;i<=nsites;i++)
    		list_site(i);
    }
    
    //MAIN FUNCTION
    int main(void)
    {
    	printf("\nLIST ALL SITES\n");
    	list_sites();
    	return 0;
    }
    On start up data is input to the array "site" from a file and the number of records entered is counted in "nsites". I wish to print out the records alphabetically by "site[x].name". It would be acceptable to just sort the array into order then print it out with the above method. I have searched around on google but can't find anything of much help.

    Apperciate any help or advice, cheers
    Andy.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I have searched around on google but can't find anything of much help.
    That's a bit odd. Sorting an array of structures based on a single field is a common question and has a common answer:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define LENGTH(x) (sizeof x / sizeof *x)
    
    struct test {
      char name[40];
      int age;
    };
    
    int cmp_name(const void *a, const void *b)
    {
      const struct test *sa = a;
      const struct test *sb = b;
    
      return strcmp(sa->name, sb->name);
    }
    
    int cmp_age(const void *a, const void *b)
    {
      const struct test *sa = a;
      const struct test *sb = b;
    
      if (sa->age < sb->age)
        return -1;
      else if (sa->age > sb->age)
        return +1;
      else
        return 0;
    }
    
    int main(void)
    {
      struct test my_test[] = {
        {"Julienne", 21}, /* I wish ;) */
        {"Tom", 29},
        {"Generic Guy", 40},
        {"Generic Girl", 40},
        {"Other People", 33},
        {"Too lazy to think up another", 13},
      };
      int i;
    
      qsort(my_test, LENGTH(my_test), sizeof my_test[0], cmp_name);
      for (i = 0; i < LENGTH(my_test); i++)
        printf("%s -- %d\n", my_test[i].name, my_test[i].age);
      printf("\n");
    
      qsort(my_test, LENGTH(my_test), sizeof my_test[0], cmp_age);
      for (i = 0; i < LENGTH(my_test); i++)
        printf("%s -- %d\n", my_test[i].name, my_test[i].age);
      printf("\n");
    
      return 0;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting an array alphabetically?
    By bluebob in forum C Programming
    Replies: 7
    Last Post: 03-09-2011, 04:53 AM
  2. Sorting an array alphabetically - split
    By wurzull in forum C Programming
    Replies: 5
    Last Post: 04-07-2007, 11:17 PM
  3. sorting alphabetically
    By ahming in forum C Programming
    Replies: 8
    Last Post: 05-26-2005, 10:34 AM
  4. Sorting inputed letters alphabetically...
    By IanelarAzure in forum C++ Programming
    Replies: 6
    Last Post: 09-30-2002, 08:56 PM