How would I go about sorting a string of letters in to alphabetical order please and thanks?
p.s without using System if possible!
How would I go about sorting a string of letters in to alphabetical order please and thanks?
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
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.
Ohhhhh Okay. I see now. That has cleared up the misunderstanding. Thank you very much