ASCII sorting
Solved
Sabetodo
Posted messages
127
Status
Member
-
Sabetodo Posted messages 127 Status Member -
Sabetodo Posted messages 127 Status Member -
Hello,
Hi, I'm trying to sort the command line arguments in ASCII order on Unix (Ubuntu) but I'm having trouble. Please, help me guys.
Here's my code:
Example:
$>./a.out test "This is a test" retest | cat -e
./a.out$
This is a test $
retest$
test$
$>
Hi, I'm trying to sort the command line arguments in ASCII order on Unix (Ubuntu) but I'm having trouble. Please, help me guys.
Here's my code:
int main( int argc, char **argv) { int i,j; char* temp; for(i=0; i<argc -1 ; i++) { for(j = i+1 ; j<argc; j++) { if(argv[ i ] > argv[ j ]) { temp = argv[ i ]; argv[i] = argv[ j ]; argv[j] = temp; } } } for(i=0; i<argc; i++) { my_putstr(argv[i]); my_putstr("\n"); } return(0); } Example:
$>./a.out test "This is a test" retest | cat -e
./a.out$
This is a test $
retest$
test$
$>
Configuration: Linux Firefox 1.0.7
4 answers
-
Hello
There is (at least) one problem with your comparison:argv[ i ] > argv[ j ]
argv[i] and argv[j] are pointers, you are comparing the values of the pointers and not the pointed strings
To compare the pointed strings, you need to use the strcmp() function (I'm quoting from memory, check the details in your C manual)