Sorting a string in to alphabetical order (a-z)?

How would I go about sorting a string of letters in to alphabetical order please and thanks?:slight_smile:

p.s without using System if possible!

http://www.dotnetperls.com/alphabetize-string Not sure what you mean by “without using System”. The functions are there anyway, might as well just use them.

–Eric

1 Like

Sorry to tell ya GeekStories… but the fact you’re using ‘string’ means you’re using ‘System’.

The word ‘string’ in C# is just a syntactical shortcut for ‘System.String’:
https://msdn.microsoft.com/en-us/library/system.string(v=vs.110).aspx

Also, I agree with Eric5h5, use the tools that are there. Sorting actually is NOT trivial… there’s a reason .Net/mono build in sorting algorithms for you. So you don’t have to implement them yourself.

The option to do so is there… but only really do so if the existing Sort algorithm is slow in the context of your use case, and you have a faster sort algorithm.

Hmm alright. I’ll take a look. thanks!

Oh. Darn okay. thanks though!

Maybe you should explain why you don’t want to use System, since that’s not something which makes much sense. You can’t use Unity without it.

–Eric

It stopped the Random.Range from being usable. And System.Random wasn’t much use for what I required.
I’ve figured it out now and got it working:)

No, it doesn’t do that. Either 1) Don’t import the System namespace, and specify it manually (i.e. System.Random instead of just Random), or 2) Specify UnityEngine.Random in order to disambiguate from System.Random.

–Eric

Really? I dunno, when I used System all Random.Range went red and gave me an error saying it couldn’t be used.
Hmm. UnityEngine.Random. I’ll try that next time if need be!

Yes really, if you import the System namespace then Unity doesn’t know what Random means by itself, since there are two of them, so you have to specify which one. That’s all.

–Eric

Huh… Alright. Well, thanks!

GeekStories, I think this might be resulting from not understanding what the namespaces are for, and what the ‘using’ commands at the top of a code file are for.

namespace -
so in a library you may have numerous classes with really generic names, like ‘Random’. The problem is if this library gets used along side another library of code that also happens to have a class with the same name, the compiler won’t know which class you’re talking about, since there are 2 of them from different libraries.

So, we get ‘namespaces’. A namespace allows you to define a sort of surname for your classes. It’s declaring “this class is from this namespace, this library of classes that are related to one another”.

In our case here we have the class ‘UnityEngine.Random’, which is a static utility class in the ‘UnityEngine’ namespace. We also have the ‘System.Random’ class from the .net/mono framework which exists in the ‘System’ namespace.

This means that all the classes actually have a much longer name… UnityEngine.Random and System.Random in these cases.

Thing is, some names can become really long.
For instance the generic list:
System.Collections.Generic.List

Or the not too often used class in Unity called:
UnityEngine.SocialPlatforms.GameCenter.GameCenterPlatform

That’s a long name… this is where using comes into play.

using -
So usually you don’t want to constantly be typing out the long winded names of things. For example:

System.Collections.List<UnityEngine.GameObject> lst = new System.Collections.List<UnityEngine.GameObject>();

It’s easier to say:

using UnityEngine;
using System.Collections.Generic;

...

List<GameObject> lst = new List<GameObject>();

This is what using is for. It’s telling the compiler to look in that namespace if it comes across any names it doesn’t recognize.

But when you do something like:

using System;
using UnityEngine;

...

Random...

Well, the compiler doesn’t recognize ‘Random’. So it looks in the used namespaces of ‘System’ and ‘UnityEngine’, and it comes back with 2 classes, one from each. It can’t tell which is which.

So, because we have namespaces to distinguish between classes from libraries, so as to avoid name collisions. And we also have a command that allows short cutting which namespaces are currently being accessed implicitly. We’ve accidentally created a situation where the names collide yet again.

SO

Don’t put in the using of BOTH System and UnityEngine at the same time. And reference the classes from the NOT included one when ever you have to directly. Usually you include the ‘using’ command for the namespace you’re referencing most in that class file.

In the case of Unity, 9 times out of 10, that will be the UnityEngine namespace. So don’t bother writing ‘using System;’. And any time you need access to a special System thing… write out the full name.

1 Like

Ohhhhh Okay. I see now. That has cleared up the misunderstanding. Thank you very much :slight_smile: