We are learning C++ at the moment and I don’t think this code requires us to use pointers yet.
So, for suggestion to the answer, please let me know if it can be done without pointer or not.
Question: How to compare an array of char so that you can sort them in ascend order?
Details:
Goal: Sort the name in ascending order as they are inserted
What we have here:
char name[1024]; // which is a part of a Struct
The insertion works. Once it is inserted. I am trying to rearrange the order so that the names are in
ascending order.
I have:
if (RecordCollection[i].name > RecordCollection[i+1].name) // for comparing
I think this might be where the problem is? Can C++ compare like that? Like, comparing John with Amy with
that one line?
After that if statement, I am using swapping the elements so they are in correct order. For example:
If John[0] the current name is > than Amy[1], then copy John to a temporary.
Then copy Amy to index[0].
Then copy John in temporary to index[1].
While typing this question, I think I need to compare the char one by one… J with A, if not the same then
sort. If same, move onto next char until it finds something different to sort. But then I don’t know how to
get the char 1 by 1.
If you are using strings, see
strcmp.To sort your array, you can also use
qsortfrom the standard C library.