# sorting question

This is a discussion on sorting question within the C Programming forums, part of the General Programming Boards category; BK11022 BS11333 BK11111 BH46222 how do i sort this kind of thing?i treat it as a string?i got try to ...

1. ## question

BK11022
BS11333
BK11111
BH46222

how do i sort this kind of thing?i treat it as a string?i got try to sort the alphabet thing but the number behide wont be sorted help~!!!

2. Originally Posted by archriku
BK11022
BS11333
BK11111
BH46222

how do i sort this kind of thing?i treat it as a string?i got try to sort the alphabet thing but the number behide wont be sorted help~!!!
What order do you want to get after sorting?

3. Numbers can be sorted as part of a string:

char numbers[10][10];
char letters[10][10];

are both strings if the last char in each entry is the end of string char: '\0'.
Without that end of string char, each entry would be a collection of char's, but it wouldn't be a string.

Usually, you think of sorting as moving things into sorted order, but there is another way to sort things that doesn't move anything except a single number, and I'd recommend it for you, with these structs.

Why? Because otherwise you have to move every members of each struct that has to be moved, with every swap you make.

It's called "sorting by Index" and here's how it works:

You make up a small array of integers, the same size as the array you want to sort.
Then you set it up with 1,2,3,4... in order, numbers.
Then you look at your items, and see which one's are out of order, and then you swap - but *not* the items you want sorted - you swap the index number only.

When you want to see the sorted order, you print or send the output to a file, *using the Index*.

Code:
```//initialize the Index array:
for(i = 0; i < MaxSoups; i++)
Index[i] = i;

/*
Soup Cans to be Sorted        Index before sorting   Index after sorting
========================================================================
Chicken Soup                           1                    1
Vegetable Soup                         2                    4
Rice with Chicken Soup                 3                    5
Clear Broth                            4                    3
Noodles                                5                    2
*/

//Now sort the Soups by Name:

for(i = 0; i < MaxSoups - 1; i++)  {
for(j = i + 1; j < MaxSoups; j++)  {
if(strcmp(Soups[i], Soups[j]) > 0)  {
temp = Index[i];
Index[i] = Index[j];
Index[j] = temp;
}
}
}

//Now show the soups in sorted order:

for(i = 0; i < MaxSoups; i++)
printf("%s \n", Soups[Index[i]]);```
Pretty spiffy, eh?

Not to worry about the numbers. The telephone book has been sorting numbers for years, and we can, also.

Vegetable Soup was #2 on the list, and after sorting, it's #5: (of course subtract 1 for C because arrays in C start with zero, not one)

4. Originally Posted by vart
What order do you want to get after sorting?
i want to short alphabets 1st in ascending order then number

BH12345
BH51234
BI12345
BI54321

hmmm the index thing is too confusing for me oo.. i only learn programming for 2month ... 1 lesson per week...

5. Originally Posted by archriku
i want to short alphabets 1st in ascending order then number

BH12345
BH51234
BI12345
BI54321

hmmm the index thing is too confusing for me oo.. i only learn programming for 2month ... 1 lesson per week...
then just sort them as strings, use strcmp on the whole string including numbered part - and you get what you want

6. Numbers sort out lower than letters - and that's how it should be done.

12345
99999
AAAAA
aaaaa

Should be your order for the strings shown here. Numbers before letters, Uppercase letters before lowercase.

7. f
Code:
```or(i = 0; i < MaxSoups - 1; i++)  {
for(j = i + 1; j < MaxSoups; j++)  {
if(strcmp(Soups[i], Soups[j]) > 0)  {
temp = Index[i];
Index[i] = Index[j];
Index[j] = temp;
}```

can u please briefly explain what is the temp, i, j variable is? what they represent?

8. Originally Posted by archriku
f
Code:
```or(i = 0; i < MaxSoups - 1; i++)  {
for(j = i + 1; j < MaxSoups; j++)  {
if(strcmp(Soups[i], Soups[j]) > 0)  {
temp = Index[i];
Index[i] = Index[j];
Index[j] = temp;
}```

can u please briefly explain what is the temp, i, j variable is? what they represent?
What do you think?

--
Mats

9. Originally Posted by matsp
What do you think?

--
Mats
i think i is the number of element of the soup, Maxsoup - 1 becuz array starts with 0, ermm i think j is the second element after i since it is i + 1 so if i is the 1st soup then j is the second soup.. am i correct?but what is temp?i really dun have any idea about this

oh yea does izzit the Maxsoup must be subtitute by a number?

10. So, i is the index into the array called index - we pass over ALL soups. j is used to work over the "remaining" soups - from the (i+1) to max soups - that way.
If the condition of strcmp() is true, we have found a soup that is in the wrong order. So we need to swap them.

Now, imagine that you and a friend is carrying numbers. Numbers are so heavy that you can only hold one. You are standing next to your friend, but the numbers are in the wrong order, so you need to swap with your friend. But neither of you can hold two numbers at once. How are you going to solve it? You need a second friend. Let's call him temp - temp holds your number, you take your friends number, and temp gives your number to your friend. That is how the swap works inside the if-statement.

--
Mats

11. Originally Posted by matsp
So, i is the index into the array called index - we pass over ALL soups. j is used to work over the "remaining" soups - from the (i+1) to max soups - that way.
If the condition of strcmp() is true, we have found a soup that is in the wrong order. So we need to swap them.

Now, imagine that you and a friend is carrying numbers. Numbers are so heavy that you can only hold one. You are standing next to your friend, but the numbers are in the wrong order, so you need to swap with your friend. But neither of you can hold two numbers at once. How are you going to solve it? You need a second friend. Let's call him temp - temp holds your number, you take your friends number, and temp gives your number to your friend. That is how the swap works inside the if-statement.

--
Mats

ic

so lets have i have 4 matrix number BK12345 BK54321 BX12345 BX54321

i declare them as
Code:
` char matrix[10], temp;`
so i do my sorting code lidis?

Code:
```for ( i = 0; i < 4; i++);
for( j = i + 1 ; j < 5; j++); {
if(strcpm(matrix[i],matrix j)>0) {
temp = matrix[i]
matrix[i] = matrix[j]
matrix[j] = temp
}```
but then Adak said the BK123123 in 2D array matrix[10][10]

so how i write for the matrix[i]?

12. A string "BK12345" is indeed a character array in itself, and you could not fit that in a single 10 character array like you declare in your post.

Also, for copying strings, you need to use strcpy(), otherwise it won't work.

--
Mats

13. Originally Posted by matsp
A string "BK12345" is indeed a character array in itself, and you could not fit that in a single 10 character array like you declare in your post.

Also, for copying strings, you need to use strcpy(), otherwise it won't work.

--
Mats
so if it can fit then i declare it ass Matrix[10][10]

then how do i do the sorting? matrix[i][?]

sorry i m very very new to programming..

strcpy like that?

strcpy(temp, matrix[i]);
strcpy(matrix[i], matrix[j]);
strcpy(matrix[j], temp);

14. Yes, your strcpy() looks right. Of course, temp needs to be the same size as the second size in matrix.

--
Mats

15. Originally Posted by matsp
Yes, your strcpy() looks right. Of course, temp needs to be the same size as the second size in matrix.

--
Mats
but then can u answer me how i do the sorting for matrix[10][10]?

cuz what i can do is single array matrix[i] but for matrix[i][?]<<< what i do with the second array

Page 1 of 4 1234 Last