1

I would like to sort an array alphabetically that looks like this:

I have tried various methods, but still no way. it crashes, and no idea yet why. Would you have some tips to start with this issue?

Since that I am not so much expert, the code is aimed at being simple (to read/understand) and doing the job.

thx, Regards

/* note: datalist[...] is basically the ouput from a sort of ls (dir
read) that it unsorted. So that: datalist[0] is ".." datalist[1] is
"file2.c" datalist[2] is "file34.c" and so on.*/


    char datalist[500][2024] ; 


void sortData(int aoptiondt)  {   ///////////////////////////LOCAL
DECLARATIONS/////////   int MAX = 500 ;    int current; int walker;  
int smallestIndex;   char* temp;

  ///////////////////////////DEFINITIONS//////////////////



    for (current = 0; current < MAX - 1; current++)
    {
      smallestIndex = current;
      for (walker = current; walker < MAX ; walker ++)
      {
        if (strcmp(datalist[walker], datalist[smallestIndex]) < 0)
          smallestIndex = walker;
      } //for walker

      //Swap to position smallest at what is the current position
      strncpy( temp , datalist[current] , PATH_MAX);
      strncpy( datalist[current] , datalist[smallestIndex] , PATH_MAX);
      strncpy( datalist[smallestIndex] , temp, PATH_MAX);
    } //for current    }

  return; }


//blabla
    int  main() {



    }
3
  • 2
    Well it looks like a main and an array datalist, nothing more than that. There is no trace of your various methods. Commented Feb 20, 2013 at 7:18
  • Welcome to Stackoverflow. We will be glad to help you if you share your code and ask questions about it. Commented Feb 20, 2013 at 7:18
  • "Welcome to Stackoverflow. We will be glad to help you if you share your code and ask questions about it. – BasicWolf 1 hour ago". thanks. I have just edited the above code (since I needed few min to re-test and re-compile) Commented Feb 20, 2013 at 8:26

3 Answers 3

2

As always - qsort is your best friend:

#include <stdio.h>
#include <stdlib.h>

#define LENGTH 6
#define STRING_SIZE 4
#define STRINGS "eed", "abd", "cde", "abc", "acd", "ade"

char strUnsorted[][STRING_SIZE] = {STRINGS};
char strSorted[][STRING_SIZE]   = {STRINGS};

int compare (const void * a, const void * b) {
  return strcmp(a, b);
}

int main () {

  qsort(strSorted, LENGTH, STRING_SIZE, compare);

  printf("Unsorted | Sorted\n");
  printf("-----------------\n");

  int i;
  for (i=0; i < LENGTH; i++)
    printf ("  %s    |   %s\n", strUnsorted[i] ,strSorted[i]);

  return 0;
}
Sign up to request clarification or add additional context in comments.

11 Comments

I think your compare function is wrong. qsort passes pointer to the places in the array to compare, so a and b are actually of type char * const *
const void * will be promoted to const char * in strcmp function. So it's ok.
no, it's not. because the parameters are basically char** not char* :)
Simply put printf("%s,%s\n", a, b); into compare function and tell me - What you see in output. Something similar to this or not ?
the output of printf is not the best argument ever, given that the example in the manpage of qsort (linux.die.net/man/3/qsort) describes my point exactly.
|
1

You don't just need an array of characters, you need an array of character arrays. As usual with C, you need to be careful about memory management, and staying with the bounds of your allocated arrays. See below for my solution. I've used a simple selection sort algorithm, and it works perfectly well for an application such as this. Here's how it's done:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 5
#define MAX_STRLEN 80

void sort(char **a, int n);
char *stringArray[N];

int main(int argc, char *argv[]) 
{
   int i;

   printf("\nEnter %d names, one per line:\n",N);
   for (i = 0; i < N; i++) 
   {
      stringArray[i] = (char *)malloc(MAX_STRLEN);
      strcpy(stringArray[i],"");
      printf("> ");
      fgets(stringArray[i],MAX_STRLEN,stdin);
      *strchr(stringArray[i],'\n') = '\0';
   }
   sort(stringArray,N);
   printf("\nSorted:\n");
   for (i = 0; i < N; i++) 
   {
      puts(stringArray[i]);
      free(stringArray[i]);
   }

   return 0;
}

/* selection sort */
void sort(char **a, int n) 
{
   int min,i,j;
   char t[MAX_STRLEN];

   for (i = 0; i < n; i++) 
   {
      min = i;
      for (j = i+1; j < n; j++) 
      {
        if (strcmp(a[j],a[min]) < 0) 
          min = j;
      }
      strcpy(t,a[min]);
      strcpy(a[min],a[i]);
      strcpy(a[i],t);
   }
}

2 Comments

+1 for the effort, however, home-brewn sort functions should only be made for practice, but not for practical use.
thx. It is indeed not so easy to understand this proposed code. I still try to understand how it might/may work. ... Hopefully it might work..(?)
-1

http://www.asciitable.com/index/asciifull.gif here is an ascii table. if you look at this, you will see character comes in a sort. this meanyou can cast char to int and get its ascii number then you can use this number to sort them.

A sample:

  int len =strlen(word);
  int wordWeight=0;
  for(int i=0;i<len;i++)
  {
      wordWeight+=(int)word[i];
      wordWeight*=1000;
  }

by using wordWeight youcan sort them. NOTE: we are multipling ascii values to 1000, because ascii values changes between 0 and 255 but you will have problem if some words start uppercase and others start lovercase. But i think you can handle it

2 Comments

-1 : sorting integers is not easier than sorting strings. Also, you'll run in overflow errors.
General comment: overflow error is actually the main risk of all those sorting operations. It must therefore be strictly carrying a constant of the memory managemement. Since the DB (database) can be big (well, only [500][1024]), memory has a great importance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.