Search Unity

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

Discussion in 'Scripting' started by GeekStories, Sep 3, 2015.

  1. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    How would I go about sorting a string of letters in to alphabetical order please and thanks?:)

    p.s without using System if possible!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    GeekStories likes this.
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    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.
     
  4. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    Hmm alright. I'll take a look. thanks!

    Oh. Darn okay. thanks though!
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    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
     
  6. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    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:)
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    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
     
  8. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    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!
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    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
     
  10. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    Huh.. Alright. Well, thanks!
     
  11. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    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<T>

    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:

    Code (csharp):
    1.  
    2. System.Collections.List<UnityEngine.GameObject> lst = new System.Collections.List<UnityEngine.GameObject>();
    3.  
    It's easier to say:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections.Generic;
    4.  
    5. ...
    6.  
    7. List<GameObject> lst = new List<GameObject>();
    8.  
    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:

    Code (csharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4.  
    5. ...
    6.  
    7. Random...
    8.  
    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.
     
    yy404 likes this.
  12. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    Ohhhhh Okay. I see now. That has cleared up the misunderstanding. Thank you very much :)