Karim Elsheikh
Karim Elsheikh

Reputation: 349

How can I sort an array of strings (2D Array) alphabetically?

I already have a list of strings read in from a text file into a 2D array named word ready to be sorted.

The list looks like:

I
like
cherry
pie
and
chocolate
pie

I want the list to look like this after sorted:

and
cherry
chocolate
I
like
pie
pie

The function prototype is below. int counter is the amount of strings, and MAX_CHAR_LEN = 1024 in case you were wondering.

void alphabetize(char word[][MAX_CHAR_LEN], int counter)
{

    return;
}

Notice that sorting by the first character alone is not sufficient, as the list contains two strings that start with "ch"

Can someone provide a function that can do this? Thanks in advance.

Upvotes: 1

Views: 9891

Answers (4)

jxh
jxh

Reputation: 70392

You want to use the qsort() function.

qsort(base, num_of_elements, element_size, my_compare);

The comparison function my_compare takes two arguments, each a const void *, and returns a number indicating the relative order of the arguments. A negative number means the first argument is before the second argument. A positive number means the first argument is after the second argument. A zero is returned if the arguments have compared to be equal.

As your string comparison is case insensitive, you will need to create your own comparison function, or find one provided to you by your system that is not part of the C library proper. POSIX provides strcasecmp() for this purpose (Google tells me that _stricmp() is available on Windows).

int my_compare (const void *a, const void *b) {
    return strcasecmp(a, b);
}

Defining the comparison function is usually the trickiest part of using qsort(). You have to understand the context of the pointers that are being passed into that function. When an array of TYPE is passed into qsort(), it will pass a pointer to const TYPE to each argument of the comparison function.

In your case, you would be passing in an array of array of MAX_CHAR_LEN chars. So, each argument to the comparison function is a pointer to const array of MAX_CHAR_LEN chars. This means that technically, the my_compare function should be written like this:

int my_compare (const void *a, const void *b) {
    typedef char TYPE[MAX_CHAR_LEN];
    const TYPE *aa = (const TYPE *)a;
    const TYPE *bb = (const TYPE *)b;
    return strcasecmp(*aa, *bb);
}

The cast on the arguments would normally not be necessary, except that C doesn't really support the notion of a constant array. It converts such a thing into an array of constants, so the cast is required to reflect that.

However, the address of an array is equal to the address of its first element. That is, for the code above, the following assertions would be true:

    assert(aa == (const void *)*aa);
    assert(bb == (const void *)*bb);

So, because the dereference of a pointer to an array equals the decayed address value of the same array, the first implementation of my_compare() is sufficient for your 2-D array.

Upvotes: 2

Shumail
Shumail

Reputation: 3143

qsort is Good Option. See it's detail here

You can also try Bubble Sort. It's implementation in C is easy - See this Good answer for help

Upvotes: 0

sora0419
sora0419

Reputation: 2378

If you want to write your own sort function, something like this is pretty straight forward.

for (int i = 0; i < array.size(); i++)
{
    for (int j = i+1; j < array.size(); j++)
    {
        if (array[i] > array[j])
            swap(array[i],array[j]);
    }
}

Upvotes: 0

jh314
jh314

Reputation: 27802

You can use the qsort function to sort. You also need to create a compare function that compares two arrays of chars, and then pass that function pointer as an argument.

Example that sorts ints:

/* qsort example */
#include <stdio.h>      /* printf */
#include <stdlib.h>     /* qsort */

int values[] = { 40, 10, 100, 90, 20, 25 };

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

int main ()
{
  int n;
  qsort (values, 6, sizeof(int), compare); 
  for (n=0; n<6; n++)
     printf ("%d ",values[n]);
  return 0;
}

The above code can easily be adapted to sort arrays of chars instead of ints.

Upvotes: 1

Related Questions