2017-05-03 96 views
0

我需要幫助按字母順序排列已讀入列表框的文本文件。有沒有一種可能具有列表框中排序這組數據的任何方式這是一個文本文件包含:C# - 按字母順序排序列表框

pizza, margherita, regular, 4.40, dough, 1.00, sauce, 0.25, mozzarella, 0.75 
pizza, margherita, large, 5.40, dough, 1.44, sauce, 0.36, mozzarella, 1.08 
pizza, margherita, extra-large, 7.50, dough, 1.79, sauce, 0.45, mozzarella, 1.34 
pizza, pepperoni, regular, 5.00, dough, 1.00, sauce, 0.25, mozzarella, 0.75, pepperoni, 2.0 
pizza, pepperoni, large, 6.00, dough, 1.44, sauce, 0.36, mozzarella, 1.08, pepperoni, 2.88 
pizza, pepperoni, extra-large, 10.00, dough, 1.79, sauce, 0.45, mozzarella, 1.342, pepperoni, 3.58 
pizza, hawaiian, regular, 5.50, dough, 1.00, sauce, 0.25, mozzarella, 0.75, ham, 1.50, pineapple, 0.5 
pizza, hawaiian, large, 6.50, dough, 1.44, sauce, 0.36, mozzarella, 1.08, ham, 2.16, pineapple, 0.72 
pizza, hawaiian, extra-large, 11.00, dough, 1.79, sauce, 0.45, mozzarella, 1.34, ham, 2.69, pineapple, 0.90 
pizza, vegetable, regular, 5.40, dough, 1.00, sauce, 0.25, mozzarella, 0.75, olives, 0.75, spinach, 0.25, mushrooms, 1.00 
pizza, vegetable, large, 6.40, dough, 1.44, sauce, 0.36, mozzarella, 1.08, olives, 1.08, spinach, 0.36, mushrooms, 1.44 
pizza, vegetable, extra-large, 10.00, dough, 1.79, sauce, 0.45, mozzarella, 1.342, olives, 1.34, spinach, 0.45, mushrooms, 1.79 
pizza, meaty, regular, 5.50, dough, 1.00, sauce, 0.25, mozzarella, 0.75, chicken, 0.50, beef, 0.50, ham, 0.25, pepperoni, 0.25 
pizza, meaty, large, 6.50, dough, 1.44, sauce, 0.36, mozzarella, 1.08, chicken, 0.72, beef, 0.72, ham, 0.36, pepperoni, 0.36 
pizza, meaty, extra-large, 13.00, dough, 1.79, sauce, 0.45, mozzarella, 1.34, chicken, 1.08, beef, 1.08, ham, 0.45, pepperoni, 0.45 
burger, beef, regular, 2.30, bun, 1, beef patty, 1 
burger, beef, large, 3.40, bun, 1, beef patty, 2 
burger, chicken, regular, 3.00, bun, 1, chicken fillet, 1 
burger, chicken, large, 4.10, bun, 1, chicken fillet, 2 
burger, vegetarian, regular, 2.50, bun, 1, falafel, 1 
burger, vegetarian, large, 3.60, bun, 1, falafel, 2 
sundry, chips, regular, 1.20, chips, 1 
sundry, onion-rings, regular, 1.70, onion rings, 1 
sundry, coleslaw, regular, 1.00, coleslaw, 
+3

哪一個領域的命令? –

+2

https://msdn.microsoft.com/en-us//library/system.windows.forms.listbox.sort(v=vs.110)。aspx –

+2

你的堆棧裏堆滿了比薩餅。 :) – dotNET

回答

1

要建立在羅布的回答,您也可以創建在FoodItem類知道如何從一個逗號分隔的字符串創建FoodItem的靜態方法。您可以在閱讀文件時調用此方法,以簡化生成食品列表。

此外,覆蓋這些類的ToString()屬性使得顯示項目也變得更容易。

這裏的FoodItem類有一些補充:

public class FoodItem 
{ 
    public string FoodCategory { get; set; } 
    public string FoodType { get; set; } 
    public string Size { get; set; } 
    public double Price { get; set; } 
    public List<Ingredient> Ingredients { get; set; } 

    public FoodItem() 
    { 
     Ingredients = new List<Ingredient>(); 
    } 

    public static FoodItem CreateFromCommaString(string commaSeparatedValues) 
    { 
     var foodItem = new FoodItem(); 
     if (string.IsNullOrWhiteSpace(commaSeparatedValues)) return foodItem; 

     var values = commaSeparatedValues.Split(',') 
      .Select(value => value.Trim()).ToList(); 

     double price; 
     foodItem.FoodCategory = values[0]; 
     if (values.Count > 1) foodItem.FoodType = values[1]; 
     if (values.Count > 2) foodItem.Size = values[2]; 
     if (values.Count > 3 && double.TryParse(values[3], out price)) 
     { 
      foodItem.Price = price; 
     } 

     if (values.Count > 4) 
     { 
      for (int i = 4; i < values.Count; i += 2) 
      { 
       var ingredient = new Ingredient {Name = values[i]}; 
       double qty; 
       if (values.Count > i + 1 && double.TryParse(values[i + 1], out qty)) 
       { 
        ingredient.Quantity = qty; 
       } 
       foodItem.Ingredients.Add(ingredient); 
      } 
     } 

     return foodItem; 
    } 

    public override string ToString() 
    { 
     return string.Format("{0}: {1} ({2}) = ${3:0.00}. Contains: {4}", 
      FoodCategory, FoodType, Size, Price, string.Join(", ", Ingredients)); 
    } 
} 

而且Ingredient類與ToString覆蓋:

public class Ingredient 
{ 
    public string Name { get; set; } 
    public double Quantity { get; set; } 
    public override string ToString() 
    { 
     return $"{Name}: {Quantity}"; 
    } 
} 

然後,填充類的列表非常簡單:只得到所有文件行,併爲每個文件生成一個新的FoodItem並將其添加到您的列表中。而一旦這樣做了,你可以訂購由多個字段列表,使用OrderByThenBy

private static void Main() 
{ 
    var filePath = @"f:\public\temp\temp.txt"; 

    var foodItems = new List<FoodItem>(); 

    foreach (var fileLine in File.ReadAllLines(filePath)) 
    { 
     foodItems.Add(FoodItem.CreateFromCommaString(fileLine)); 
    } 

    var sortedItems = foodItems 
     .OrderBy(item => item.FoodCategory) 
     .ThenBy(item => item.FoodType) 
     .ThenBy(item => item.Price) 
     .ToList(); 

    sortedItems.ForEach(Console.WriteLine); 

    Console.Write("\nDone!\nPress any key to exit..."); 
    Console.ReadKey(); 
} 

輸出(點擊放大):

enter image description here

2

啓用Sorted屬性爲true

listBox1.Sorted = TRUE;

1

無論如何你可以改變你的價值存儲方式嗎?

例如。你有

漢堡,素食,大,3.60,麪包,1,沙拉三明治,2在一個逗號分隔線。

如何

漢堡,素食主義者,大,3.60,包子:1 |沙拉三明治:2,那麼你會知道你有 產品,型號,規格,價格,成分 和配料是 '|'分開,還有名字和數字之間的':'。無論如何,如果它被設置爲可以將它讀入對象列表或字典中,那麼列表框中的每個項目都可以具有Key作爲值,那麼您可以使用值中的其他值代碼很容易。

public class FoodItem 
{ 
    public string FoodCategory {get;set;} 
    public string FoodType {get;set;} 
    public string Size {get;set;} 
    public double Price {get;set;} 
    public List<Ingredient> Ingredients {get;set;} 
} 

public class Ingredient 
{ 
    public string Name {get;set;} 
    public int Quantity {get;set;} 
} 

然後,當您閱讀您的文本字段時,將每行解析爲一個食物項目的實例。

另一個問題是逐行讀取文本文件。

https://stackoverflow.com/a/7980605/7683170