2016-09-10 4 views
-1

文字列の文字列が好きなときには、 A = 0、B = 1のように...どのように文字をアルファベット順に並べて表示するのか

私はいくつかのコードを作ったが、私は間違った答えを得て、どこに問題があるか知っていると思うが、何が問題なのか分からない。だからここ

はコードです:

void Start() 
{ 
    i = Inventory.instance; 
    List<Items> items = i.returnList("Food"); 
    Debug.Log(items.Count); List<Items> sItems = GetListSetList(items); 

    foreach (Items item in sItems) 
    { 
     Debug.Log(item.name); 
    } 
} 

リスト項目= i.returnList( "食品")。 < - この部分は、私が在庫を求める場所で、すべての "食べ物"のテージドアイテムを返します。コードはここにあります:

public List<Items> returnList(string tag) 
{ 
    List<Items> classifiedItemSet = new List<Items>(); 
    foreach (Items i in items) 
    { 
     if (i.tag == tag) 
     { 
      classifiedItemSet.Add(i); 
     } 
    } 
    return classifiedItemSet; 
} 

リストsItems = GetListSetList(items); < - これは私が実際に在庫から得たものを短いリストにしようとする部分です。これは次のようになります。

private List<Items> GetListSetList(List<Items> itemS) 
{ 
    foreach (Items item in itemS) 
    { 
     GetAlphabetaValue(item); 
    } 
    List<Items> shorted = new List<Items>(); 
    Items[] hold = new Items[itemS.Count]; 
    foreach (Items item in itemS) 
    { 
     for (int x = 0; x < hold.Length; x++) 
     { 
      if (hold[x] == null) 
      { 
       hold[x] = item; 
       break; 
      } 
      else 
      { 
       bool PassIt = true; 
       for (int c_value = 0; c_value < item.Alphabet_value.Length; c_value++)      
        if (item.Alphabet_value[c_value] > hold[x].Alphabet_value[c_value])       
         PassIt = false;      

       if (PassIt) 
       { 
        for (int h_pos = hold.Length - 1; h_pos > x; h_pos--)      
         if (hold[h_pos] != null) 
          hold[h_pos] = hold[h_pos - 1];      

        hold[x] = item; 
        break; // If i use this break i get error ("NullReferenceException") 
       } 
       else continue;        
      } 
     } 
    } 
    for (int x = 0; x < hold.Length; x++)   
     shorted.Add(hold[x]);   
    return shorted; 
} 

私はすべてのアイテム名文字列のすべての文字に値を与えます。それはこの部分です:GetAlphabetaValue(item);私はあなたが私が話をしてみてください何を理解してほしい

private void GetAlphabetaValue(Items x) 
{ 
    x.Alphabet_value = new int[x.name.Length]; 
    for (int c = 0; c < x.Alphabet_value.Length; c++) 
    { 
     string character = x.name.Substring(c, 1); 
     character.ToLower(); 
     switch (character) 
     { 
      case "a": 
       x.Alphabet_value[c] = 0; 
       break; 
      case "b": 
       x.Alphabet_value[c] = 1; 
       break; 
      case "c": 
       x.Alphabet_value[c] = 2; 
       break; 
      case "d": 
       x.Alphabet_value[c] = 3; 
       break; 
      case "e": 
       x.Alphabet_value[c] = 4; 
       break; 
      case "f": 
       x.Alphabet_value[c] = 5; 
       break; 
      //To the end 
     } 
    } 
} 

:ああと申し訳ありませんが私は、この値を取得する方法、それをAlphaBeta :)オーケーに名前を付けることはこれであるDは、ありがとう:)と私は起動する前に、インターネットでいくつかの情報を見つけるしてみてくださいこれを行うが、私は本当にどのように配列から複数の文字列を短くすることができます何かを見つけることはありません。

私が間違っだと思いますが、今私はちょうど何を間違った聖霊降臨祭のことがわかりカントこの部分:

for (int h_pos = hold.Length - 1; h_pos > x; h_pos--) 
     if (hold [h_pos] != null) 
     { 
     hold [h_pos] = hold [h_pos - 1];       
     hold [x] = item; 
     }   

答えて

0

がで起動するには、あなたが本当にそれは20 switch-caseを持っているのは良いアイデアだと思いますか?

文字はすでに番号が付けられています。a-zは、97-122の文字に対応しています。Unicodeになります。

この機能:

void GetAlphabetaValue(Items x) 
{ 
    x.Alphabet_value = new int[x.name.Length]; 
    for (int c = 0; c < x.Alphabet_value.Length; c++) 
    { 
     string character = x.name.Substring (c, 1); 
     character.ToLower(); 
     switch (character) 
     { 
     case "a": 
     x.Alphabet_value [c] = 0; 
     break; 
     ///Etc... //Etc..... And end. 
     } 
    } 
}  

はこのようになります。

void GetAlphabetaValue2(Items x) 
{ 
    var CharIntList = List<int>; 
    foreach (char ch in x.Name) 
     CharIntList.Alphabet_value.Add(ch - 97); 
    x.Alphabet_value = CharIntList.ToArray(); 
} 

はるかに簡単。また、コードは面倒で、理解しにくく、フォーマットが悪いです。おそらくあなたはC#の初心者なので、コードを書く方法を読んでみてください。うまくやっているようではありませんが、他のpepoleがコードを理解できるはずです。

あなたの質問については、sortにはないと思いますが、short(完全に異なるもの)です。また、Itemsは複数でもありますか?それは単なるものです、それはItemでなければなりません。

まあ、私はあなたの問題を知らないが、あなたはこれまであなたGetListSetList()機能を置き換えることができます。

private List<Items> GetListSetList(List<Items> items) 
{ 
    foreach (Items item in items) 
    { 
     GetAlphabetaValue(item); 
     Array.Sort(item.Alphabet_value); 
    } 
    return items; 
} 
+0

ワウ、ありがとうございました。私は、より良い方法がマルチスイッチの場合のステートメントよりもそうであると考えていました。 –

+0

'int [] Alphabet_value'の値を' sort'するコードですか? – null

+0

そして、最後の部分は何度も見ていますが、私はクラスの指定された部分(item.Alphabet_value)を使用することができないことに気付きません。私はショートショート(アイテム)のように動作すると思っていた。 :) –

0

この

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      List<Items> items = Items.returnList("Food"); 
      var groups = items.GroupBy(x => x.name.Substring(0,1)).ToList(); 

     } 
    } 
    public class Items 
    { 
     public static List<Items> items = new List<Items>(); 
     public string name { get; set; } 
     public string type { get; set; } 


     public static List<Items> returnList(string type) 
     { 
      return items.Where(x => x.type == type).ToList(); 
     } 

    } 

} 
+0

ありがとう。その良いforeachループを取り除く:) –

1

使用このコードのようなものを試してみてください。

 string str = "Tamil"; 
     List<char> list = str.ToList(); 
     list = list.OrderBy(x => x.ToString()).ToList(); 
     foreach (var item in list) 
     { 
      Console.WriteLine(item); 
     } 
     Console.ReadLine(); 
関連する問題