Since this is a homework question, I'll given an enabling answer rather than the actual code version...
Approach to working out the algorithm
- Get a sheet of paper, rip it into several pieces, then write one word on each piece.
- Put the bits of paper one after the other (e.g. so they represent an unsorted array)
- Work your way through the list putting items in order. Try to take a procedural approach; i.e. so you're obeying a rule each time you make a decision, rather than "magically"/intelligently jumping to the next step.
- Try to write down the rules you're following.
- Jumble up the list again, and repeat, exclusively following the rules you've written; does it work, or are you missing any rules / are there rules which didn't make sense.
- At this point your list of rules is essentially pseudo-code; i.e. you have a set of instructions which can be followed to get your result; but it's not yet written in any recognised programming language.
- Translate those rules / the pseudo-code to actual code.
- Try it out. If it doesn't work, try to work out where it's going wrong. If you can't see what's wrong, come on here, post that code, and state your issue; we can then help get you back on the right path.
NB: You can of course look up existing sorting algorithms and implement those; but personally I prefer the above since it teaches you to think for yourself, rather than translating an existing algorithm into your particular coding language.
Other Hints
When making comparisons, consider what you need to do. i.e. You've asked above about converting the characters in the strings to numbers before comparing them. Is that step required; could you not simply compare the strings alphabetically? https://www.google.co.uk/search?q=c%23+compare+strings+alphabetically
In the code sample you've provided you have values string1
and string2
, but you never show how their values' are assigned... Do you need to assign a value to a variable before you can compare it / when and where should those assignments take place?
You also have a loop with integer j
being assigned... Where are you making use of that value / what's the point of assigning it a value unless you're going to use it?
Likewise you have a variable biggestFirstChar
which you assign a value, but don't do anything with. Is that needed / where would you use it?
The result you're after is a sorted list of values, which you then want to output. Where are you holding that result / where's the code to output the results in order?
How do you want to build up that result list; i.e. are you able to populate it all at once, or do you have to modify it bit by bit until all items are in order?
Additionally, break the issue down into chunks and test each chunk and/or think about the implications. i.e.
- How do you produce output?
- How do you compare 2 strings to determine which should be first in a list?
- How do you output an array (list)?
- How do you add items to your result list (or how do you swap values in an existing list if you're working on the original)?
- Given an array (list) with 2 elements (items), write code to put them in order. Ensure this works regardless of the initial order (
Aardvark, Balloon
and Balloon, Aardvark
).
- Now extend this to work for a list of 3 items; but ensure your solution will still work if given a list of 2 items.
- Now try to solve the original issue
- Finally (optional; for bonus points) look at your code and see if you can make it better (i.e. refactor it). This means looking at simple presentation (have you indented your code / are you using meaningful variable and function names, did you comment anything which isn't self explanatory, etc), and also looking for ways to improve it (can you see something inefficient where a different approach may work better / is any code repeated which could be moved out into a different function and called from all the places it's required?