for(j=0;j<3;j++)
{

repeat:if((int*)ch[j][l]>(int*)ch[j+1][l])
{
strcpy(tmp,ch[j]);
strcpy(ch[j],ch[j+1]);
strcpy(ch[j+1],tmp);
}
else if((int*)ch[j][l]==(int*)ch[j+1][l])
{
l++;
}       goto repeat;

}

for(i=0;i<3;i++)
{
printf("%s",ch[i]);
}

return 0;

NOt gettin any output

Edited by mike_2000_17: Fixed formatting

3
Contributors
2
Replies
5
Views
7 Years
Discussion Span
Last Post by navedalam

That's a very odd sort block of code. I'd dump it. Here's an easy one (this is substitution sort and should be used for small and medium sorting jobs).

Where SIZE is the size of the array with data you need sorted
where i and j are integers
where you have a 2D string array[SIZE][30]; //approx. on the 30
where you have a 1D char array[30]; //holds just one name at any time

for(i = 0;i < SIZE-1; i++) {
for(j = i+1; j <SIZE; j++) {
if((strcmp(array[i], array[j])) > 0) { //out of order?
strcpy(temp, array[i]); //to temp
strcpy(array[i], array[j]); //j to i
strcpy(array[j], temp); //temp to j
}
}
}
printf("\nIn sorted order:\n");
for(i = 0; i < SIZE; i++)
printf("%s\n", array[i]);

This will sort according to the ASCII values (OK, the character set values you have on your system, but normally they're the same as ASCII values.

This is not exactly the same as a lexicographic sort (like the Dictionary). Close, but not quite the same.

Avoid using goto in your code.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.