C#-按字母顺序排序列表框
我需要帮助排序一个文本文件的字母顺序已经读到一个列表框。是否有任何方法可以让列表框对文本文件中包含的这组数据进行排序:C#-按字母顺序排序列表框,c#,sorting,listbox,C#,Sorting,Listbox,我需要帮助排序一个文本文件的字母顺序已经读到一个列表框。是否有任何方法可以让列表框对文本文件中包含的这组数据进行排序: 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,
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,
将排序属性启用为true
listBox1.Sorted=true 将排序属性启用为true
listBox1.Sorted=true 您是否可以更改存储值的方式 比如说。你有 汉堡,素食者,大号,3.60,面包,1,沙拉三明治,2都在一个逗号分隔的行中 怎么样 汉堡,素食者,大号,3.60,面包:1 |沙拉三明治:2那么你就会知道你有 产品、类型、尺寸、价格、成分 成分在名称和数量之间用“|”分隔,也用“:”分隔 无论如何,如果它是这样设置的,您可以将它读入对象列表或字典中,这样列表框中的每个项目都可以使用键作为值,那么您可以在代码中的其他位置轻松使用这些值
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;}
}
公共类FoodItem
{
公共字符串FoodCategory{get;set;}
公共字符串FoodType{get;set;}
公共字符串大小{get;set;}
公共双价{get;set;}
公共列表成分{get;set;}
}
公共类成分
{
公共字符串名称{get;set;}
公共整数数量{get;set;}
}
然后,当您读取文本字段时,将每一行解析为food item的一个实例
另一个问题是逐行阅读文本文件
您是否可以更改存储值的方式 比如说。你有 汉堡,素食者,大号,3.60,面包,1,沙拉三明治,2都在一个逗号分隔的行中 怎么样 汉堡,素食者,大号,3.60,面包:1 |沙拉三明治:2那么你就会知道你有 产品、类型、尺寸、价格、成分 成分在名称和数量之间用“|”分隔,也用“:”分隔 无论如何,如果它是这样设置的,您可以将它读入对象列表或字典中,这样列表框中的每个项目都可以使用键作为值,那么您可以在代码中的其他位置轻松使用这些值
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;}
}
公共类FoodItem
{
公共字符串FoodCategory{get;set;}
公共字符串FoodType{get;set;}
公共字符串大小{get;set;}
公共双价{get;set;}
公共列表成分{get;set;}
}
公共类成分
{
公共字符串名称{get;set;}
公共整数数量{get;set;}
}
然后,当您读取文本字段时,将每一行解析为food item的一个实例
另一个问题是逐行阅读文本文件
要基于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));
}
}
然后,填充类列表很简单:只需获取所有文件行,并为每一行生成一个新的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();
}
private static void Main()
{
var filePath=@“f:\public\temp\temp.txt”;
var foodItems=新列表();
foreach(File.ReadAllLines(filePath)中的var fileLine)
{
Add(FoodItem.CreateFromCommaString(fileLine));
}
var sortedItems=foodItems
.OrderBy(item=>item.FoodCategory)
.ThenBy(item=>item.FoodType)
.ThenBy(项目=>item.Price)
.ToList();
sortedItems.ForEach(Console.WriteLine);
控制台。写入(“\n完成!\n按任意键退出…”);
Console.ReadKey();
}
输出(单击放大):
要基于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));
}
}
然后,填充类列表很简单:只需获取所有文件行,并为每一行生成一个新的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();
}
private static void Main()
{
var filePath=@“f:\public\temp\temp.txt”;
var foodItems=新列表();
foreach(File.ReadAllLines(filePath)中的var fileLine)
{
Add(FoodItem.CreateFromCommaString(fileLine));
}
var sortedItems=foodItems
.OrderBy(item=>item.FoodCategory)
.ThenBy(item=>item.FoodType)
.ThenBy(项目=>item.Price)
.ToList();
sortedItems.ForEach(Console.WriteLine);
控制台。写入(“\n完成!\n按任意键退出…”);
Console.ReadKey();
}
输出(单击放大):
订购的字段是什么?你的比萨饼堆满了。)第一个字段是“caterogy”,所以首先是汉堡,然后是比萨饼,然后是sundrys。找到了另一个带有超级答案的帖子。订购的字段是什么?你的比萨饼堆积如山。:)第一个字段是“caterogy”,所以首先是汉堡,然后是比萨饼,然后是sundrys。找到了另一个超级答案的帖子。