【问题标题】:C# - Sorting listbox alphabeticallyC# - 按字母顺序对列表框进行排序
【发布时间】:2017-05-03 06:55:17
【问题描述】:

我需要帮助按字母顺序对已读入列表框的文本文件进行排序。有没有办法让列表框对包含在文本文件中的这组数据进行排序:

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, 

【问题讨论】:

标签: c# sorting listbox


【解决方案1】:

启用排序属性为真

listBox1.Sorted = true;

【讨论】:

    【解决方案2】:

    要基于 Rob 的答案,您还可以在 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));
        }
    }
    

    还有带有ToString 覆盖的Ingredient 类:

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

    输出(点击放大):

    【讨论】:

      【解决方案3】:

      您是否可以更改值的存储方式?

      例如。你有

      Burger, Vegetarian, Large, 3.60, bun,1, falafel,2 都在一个逗号分隔的行中。

      怎么样

      Burger, Vegetarian, Large, 3.60, bun:1|falafel: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

      【讨论】:

        猜你喜欢
        • 2013-05-02
        • 2014-02-08
        • 1970-01-01
        • 2017-02-23
        • 2021-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多