我需要帮助按字母顺序排序已经读入列表框的文本文件。有没有办法可能让列表框对包含在文本文件中的这组数据进行排序:
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,
答案 0 :(得分:2)
启用Sorted Property为true
listBox1.Sorted = true;
答案 1 :(得分:1)
无论如何,您是否可以更改存储值的方式?
例如。
Burger,Vegetarian,Large,3.60,bun,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;}
}
然后,当您阅读文本字段时,将每行解析为食物项目的实例。
另一个问题是逐行阅读文本文件。
答案 2 :(得分:1)
为了建立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));
}
}
Ingredient
类ToString
覆盖:
public class Ingredient
{
public string Name { get; set; }
public double Quantity { get; set; }
public override string ToString()
{
return $"{Name}: {Quantity}";
}
}
然后,填充类的列表很简单:只获取所有文件行,并为每个文件行生成一个新的FoodItem
并将其添加到列表中。完成后,您可以使用OrderBy
和ThenBy
按多个字段对列表进行排序:
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();
}
输出(点击放大):