2016-09-24 4 views
0

나는 사전을 가지고있다. 모든 것이 잘 작동하지만 정렬됩니다. 나는 심지어 SortedDictionary를 시도했다.값으로 사전 정렬?

내가하고있는 일은 다음과 같습니다. FilePath, FileName alright가 포함 된 사전이 있습니다.

글쎄 나는 Value로 정렬하고 키를 목록에 넣으려고합니다.

여기 내 코드입니다. (UPDATED : 전체 방법).

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); 
    // 
    //} 
} 

좋습니다. Debug.Log()는 Unity의 Console.WriteLine 방식입니다. 그리고 실제로 그것을 분류하고 있다고 말합니다. 하지만 내가 할 때.

scriptsList.Add (item.Key); 

이전과 같이 조직화되어 있지 않습니다. 누락 된 몇 가지 간단한 단계가 있습니까? 콘솔은 사실 내가 원하는 방식으로 완벽하게 정렬되었다고 말하기 때문에. 그러나 무엇인가의 이유로, scriptsList.Add (item.key) < 파일에 대한 PATH의 경우. 분류되지 않았다고합니다.

나는 scriptList.Sort()를 사용했다. 그러나 scriptList가 키 (파일 경로)임을 기억하십시오. 그래서 저는 Values ​​(파일 이름)를 통해 정렬하려고했습니다.

또 다시 분류한다고합니다. /Cat.txt, Cat.txt :

여기

C 말하는 컴파일러의 예입니다.
C : /Dog.txt, Dog.txt.
C : /Wolf.txt, Wolf.txt.

하지만 목록에 추가 할 때.

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

+0

그냥 경로를 사용하여 경로 목록을 만드십시오. List myList = new List ()'그러면 Path.GetFileName (myList [ 색인])', 왜 두 사람이 모두 필요한지 알 수 없습니다. 당신은 단순히 그 시나리오에서'OrderBy'를 사용하여 당신의 목록을 주문할 수 있습니다. 그러나 당신이 그것을 그렇게 필요로하는지 확실하지 않습니다. – Niklas

+0

atm을 사용하고있는 것을 절대 사용 하시겠습니까? – Niklas

+0

변수'scriptList'의 종류는 무엇입니까? 'HashSet '입니까? 왜냐하면 내가'var scriptList = scriptDictionary.OrderBy (p => p.Value) .Select (p => p.Key) .ToList();'를 수행하면 순서가 정확하기 때문입니다. – dbc

답변

0

나는 그것을 알아 냈어! 나는 총 거짓말처럼 느껴져 .... 나는 if 문 중 하나의 맨 위에 코드의 오래된 부분을 남겼다. if (scriptList.Count < 1) {Sort It}. 그것을 제거하고, 지금 갈 게요 :)