# String Sort

• (3 Pages)
• 1
• 2
• 3

## 32 Replies - 1384 Views - Last Post: 24 February 2013 - 09:58 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=313362&amp;s=0288e23b7cde11644bdaf7cd57b577bb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Gagzzy

Reputation: 0
• Posts: 27
• Joined: 22-April 12

# String Sort

Posted 24 February 2013 - 01:48 PM

i am making a program to take in integer 'n' then 'n' amount of strings, i then need to sort them by length of size then each string needs to be alphabetically sorted, i can sort the strings out by length but struggling to find a way to run through each character and put them in alphabetical order can anyone shine some light?

my code for sorting out length
``` for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if((strlen(strs[i])) > strlen(strs[j]))
{
sortTemp = strs[i];
strs[i] = strs[j];
strs[j] = sortTemp;
}
}
}

```

just not sure how to make the next sort for characters.

Is This A Good Question/Topic? 0

## Replies To: String Sort

### #2 TwoOfDiamonds

• D.I.C Regular

Reputation: 54
• Posts: 272
• Joined: 27-July 12

## Re: String Sort

Posted 24 February 2013 - 01:50 PM

it's pretty much the same for letters since 'b' > 'a' , 'c'>'b' , etc.

### #3 Gagzzy

Reputation: 0
• Posts: 27
• Joined: 22-April 12

## Re: String Sort

Posted 24 February 2013 - 01:54 PM

yeah i understand that but if i sort it using

```for (int i = 0; i < n; i++)
{
for (int j = i+1; j < n; j++)
{
if (strs[i] < strs[j])
{
sortTemp = strs[i];
strs[i] = strs[j];
strs[j] = sortTemp;
}
}
}

```

it makes no difference as it just goes through each string not each strings character, thats where am stuck

### #4 TwoOfDiamonds

• D.I.C Regular

Reputation: 54
• Posts: 272
• Joined: 27-July 12

## Re: String Sort

Posted 24 February 2013 - 01:57 PM

You can treat the vector of strings as a 2d array

so string[i][j] would access the letter on the j position from the string on the i position in the vector:)

### #5 Gagzzy

Reputation: 0
• Posts: 27
• Joined: 22-April 12

## Re: String Sort

Posted 24 February 2013 - 02:05 PM

"Incompatible integer to pointer conversion assigning to 'char *' from 'char'; take the address with &"

```
for (int i = 0; i < n; i++)
{
for (int j = i+1; j < n; j++)
{
if (strs[i][j] < strs[i][j+1])
{
sortTemp = strs[i][j];
strs[i][j] = strs[i][j+1];
strs[i][j+1] = sortTemp;
}
}
}

```

### #6 TwoOfDiamonds

• D.I.C Regular

Reputation: 54
• Posts: 272
• Joined: 27-July 12

## Re: String Sort

Posted 24 February 2013 - 02:06 PM

what type is 'sortTemp' ?

### #7 Gagzzy

Reputation: 0
• Posts: 27
• Joined: 22-April 12

## Re: String Sort

Posted 24 February 2013 - 02:10 PM

char *sortTemp;

has to be done in heap memory so before all the sorting Ive had to allocate a default memory to each string then reallocate memory to the size of the string an so on, which seems to make the rest of the program tricky, took me while to get my head round it but now Ive hit a wall trying to sort out each alphabetically

### #8 TwoOfDiamonds

• D.I.C Regular

Reputation: 54
• Posts: 272
• Joined: 27-July 12

## Re: String Sort

Posted 24 February 2013 - 02:15 PM

you can declare some other variable of type char ...

char* works for the string type since strings are actually vectors of chars, but when you access the string[i][j] element you actually get an item of type char

This post has been edited by TwoOfDiamonds: 24 February 2013 - 02:16 PM

### #9 Gagzzy

Reputation: 0
• Posts: 27
• Joined: 22-April 12

## Re: String Sort

Posted 24 February 2013 - 02:21 PM

sorry am not understanding what to do from what your saying

### #10 TwoOfDiamonds

• D.I.C Regular

Reputation: 54
• Posts: 272
• Joined: 27-July 12

## Re: String Sort

Posted 24 February 2013 - 02:27 PM

Well ...

char* is actually a pointer to a char , not an actual char .
So what you are doing by
```sortTemp = strs[i][j];
```
is making the compiler try to convert the strs[i][j] which is a char to an address so it could be asigned to sortTemp .

Basically it's something like char* = 'a' ; and that is not correct for obvious reasons (look up a tutorial on pointers if it isn't obvious .

So what you could do is declare e new variable like
```char chSortTemp;
```

And use it to store the chars you get from strs[i][j]

### #11 TwoOfDiamonds

• D.I.C Regular

Reputation: 54
• Posts: 272
• Joined: 27-July 12

## Re: String Sort

Posted 24 February 2013 - 02:27 PM

Well ...

char* is actually a pointer to a char , not an actual char .
So what you are doing by
```sortTemp = strs[i][j];
```
is making the compiler try to convert the strs[i][j] which is a char to an address so it could be asigned to sortTemp .

Basically it's something like char* = 'a' ; and that is not correct for obvious reasons (look up a tutorial on pointers if it isn't obvious .

So what you could do is declare e new variable like
```char chSortTemp;
```

And use it to store the chars you get from strs[i][j]

### #12 jjl

• Engineer

Reputation: 1120
• Posts: 4,646
• Joined: 09-June 09

## Re: String Sort

Posted 24 February 2013 - 02:30 PM

If you are sorting each string in an array of strings, then you will need 3 loops (if you are using bubble sort). Create a bubble sort function that will sort a single string. Then loop through every string and call bubble sort.

i.e.
```#include <stdio.h>
#include <string.h>

void bubbleSortStr(char *str, int n) {
//implementation
}

int main() {
char *strs[10];

//allocate and initalize strings

for(int i=0; i<10; i++) {
bubbleSortStr(strs[i], strlen(strs[i]));
}

return 0;
}

```

Quote

You can treat the vector of strings as a 2d array

They are using C, not c++. A vector of c++ strings is not valid.

This post has been edited by jjl: 24 February 2013 - 02:52 PM

### #13 TwoOfDiamonds

• D.I.C Regular

Reputation: 54
• Posts: 272
• Joined: 27-July 12

## Re: String Sort

Posted 24 February 2013 - 02:33 PM

jjl, on 24 February 2013 - 11:30 PM, said:

Quote

You can treat the vector of strings as a 2d array

They are using C strings, not C++ strings, so that is not valid. T

Why not ?

After all , in C it's char[][] , right ? so it's a 2d array of chars ....
Correct me if I'm wrong please

### #14 TwoOfDiamonds

• D.I.C Regular

Reputation: 54
• Posts: 272
• Joined: 27-July 12

## Re: String Sort

Posted 24 February 2013 - 02:33 PM

jjl, on 24 February 2013 - 11:30 PM, said:

Quote

You can treat the vector of strings as a 2d array

They are using C strings, not C++ strings, so that is not valid. T

Why not ?

After all , in C it's char[][] , right ? so it's a 2d array of chars ....
Correct me if I'm wrong please

### #15 jjl

• Engineer

Reputation: 1120
• Posts: 4,646
• Joined: 09-June 09

## Re: String Sort

Posted 24 February 2013 - 02:50 PM

Quote

Why not ?

After all , in C it's char[][] , right ? so it's a 2d array of chars ....
Correct me if I'm wrong please

I was referring to the vector of c++ strings, not being valid. Yes, you can treat an array of char pointers as a 2D array.

Sorry for the confusion, I'll clean up my statement.

This post has been edited by jjl: 24 February 2013 - 02:51 PM