0

I have a dictionary. Everything is working fine but sorting. I have even tried a SortedDictionary.

Here's what I am doing, I have a Dictionary that contains FilePath,FileName alright.

Well I am trying to sort by the Value and then put then put the (Keys) into a list.

Here is my code. (UPDATED: ENTIRE METHOD).

public static void DisplayScriptListNames(){
    scriptsList.Clear ();
    fileInfo = new DirectoryInfo (Application.dataPath);

    if (EclecticGlobalSettings._cSharp && isSharp) {
        sharpFiles = fileInfo.GetFiles ("*.cs", SearchOption.AllDirectories).ToList();
    } if(EclecticGlobalSettings._usScripts && !isSharp) {
        javaFiles = fileInfo.GetFiles ("*.js", SearchOption.AllDirectories).ToList();
    }


    if (EclecticGlobalSettings._cSharp && isSharp) {
        // C#
        if (sharpFiles.Count != 0) {
            foreach (FileInfo i in sharpFiles){
                string line = i.Name.ToString ();
                string checkPath = Path.GetDirectoryName (i.FullName);
                string assetsPath = checkPath.Substring (i.FullName.IndexOf ("Assets"));
                if (!assetsPath.Contains("Editor") && !assetsPath.Contains("Standard Assets")) {
                    scriptDictionary.Add(i.FullName,i.Name);
                    scriptsFound += 1;
                } 
            }
        } 
    } 

    if(EclecticGlobalSettings._usScripts && !isSharp){
        //JS
        foreach (FileInfo i in javaFiles) {
            //string line = i.FullName.ToString ();

            string line = i.Name.ToString ();
            string checkPath = Path.GetDirectoryName (i.FullName);
            string assetsPath = checkPath.Substring (i.FullName.IndexOf ("Assets"));
            if (!assetsPath.Contains("Editor") && !assetsPath.Contains("Standard Assets")) {
                Debug.Log (i.Name);
                scriptDictionary.Add(i.FullName,i.Name);
                scriptsFound += 1;
            }
        }
    }


    foreach (KeyValuePair<string,string> item in scriptDictionary.OrderBy(key=>key.Value)) {
        Debug.Log (item);
        scriptsList.Add (item.Key);
    }
    //scriptsList.AddRange (scriptDictionary.Keys);
    //scriptsList.Sort (Path.GetFileName);
    //foreach (string ii in scriptsList) {
    //  Debug.Log (ii);
    //
    //}
}

Okay, the Debug.Log() is Unity's way of a Console.WriteLine. And it does in fact say it's sorting it. But when I do.

scriptsList.Add (item.Key);

It's unorganized as it was before. Is there some simple little step I am missing? Because the console does in fact say it's sorted perfectly the way I'd like. But for some reason, the scriptsList.Add(item.key) < For the PATH to the file. Says it isn't sorted.

I would do scriptList.Sort(); But remember, the scriptList is the keys (File Paths). Which is why I've been trying to sort via Values (the file names).

Which again, says it does sort them.

Here's an example of what compiler says

C:/Cat.txt, Cat.txt.
C:/Dog.txt, Dog.txt.
C:/Wolf.txt, Wolf.txt.

But when I go to add them to the list.

C:/Wolf.txt.
C:/Dog.txt.
C:/Cat.txt.

n1warhead
  • 19
  • 6
  • just make a list from the path by doing `List myList = new List()` then you can just get the items filename by doing `Path.GetFileName(myList[index])` , cant see why you would need both of those yet. you can simply order your listby using `OrderBy` in that scenario but im not sure if you'd need it that way. – Niklas Sep 24 '16 at 18:57
  • do you HAVE to absolutely use what you are using atm? – Niklas Sep 24 '16 at 18:59
  • What is the type of the variable `scriptList`? Is it a `HashSet`? Because if I do `var scriptList = scriptDictionary.OrderBy(p => p.Value).Select(p => p.Key).ToList();` then the order is correct. – dbc Sep 24 '16 at 18:59
  • i would suggest you take a look here : http://stackoverflow.com/q/1043039/3956100 – Niklas Sep 24 '16 at 19:04
  • Here's a [fiddle](https://dotnetfiddle.net/6OzbaC) showing OrderBy + ToList working. Can you give a full [mcve] for your problem? – dbc Sep 24 '16 at 19:05
  • Hey guys, just updated code to show entire method... And going to look into the other commends to see if I can get it working. – n1warhead Sep 24 '16 at 19:33
  • @dbc what does Dump do? I don't have that option.... Keep in mind this is for Unity. So it's I believe .Net 3.5. And it's just a string list, a list of strings for File Paths.. – n1warhead Sep 24 '16 at 19:58
  • Still having the same problem as always.... @dbc I did the codes provided (without dump) as I don't have it. And the Console definitely says they are organized, but as soon as I add them to the list, they are again unorganized. – n1warhead Sep 24 '16 at 20:04

1 Answers1

0

I figured it out guys! I feel like a total dummy.... I had left an old part of code at the top of one of my if statements, and I had, had if(scriptList.Count < 1) { Sort It }. Removed that, and now good to go :)

n1warhead
  • 19
  • 6