我想知道如何将我在列表框中的项目从txt文件中按最低编号排序到最高编号?
randomGenerator's
在我的txt文件中
private void Form1_Load(object sender, EventArgs e)
{
try
{
try
{
string[] lines = System.IO.File.ReadAllLines("file.txt");
foreach (string line in lines)
{
listBox1.Items.Add(line);
}
}
catch
{
}
}
catch
{
}
}
答案 0 :(得分:1)
你必须在dicionary中分隔字符串和数字然后按数字排序
Dictionary<string, int> dic = new Dictionary<string, int>();
foreach (var line in lines)
{
string[] str = line.Split('=');
var num= str[1].Replace("$",""); // for seperate $ and number
dic.Add(str[0], (Convert.ToInt32(num)));
}
foreach(var sortItem in dic.OrderBy(x=>x.Value))
{
listBox1.Items.Add(sortItem .Key + " = "+ sortItem .Value.ToString() + "$");
}
输出是:
socks = 5$
Shirt = 10$
hat = 10$
pants = 20$
shoes = 60$
答案 1 :(得分:0)
我们可以使用linq来执行此操作:
-- WHEN-BUTTON-PRESSED on a button labeled '1'
:block.display_box := :block.display_box || '1';
-- WHEN-BUTTON-PRESSED on a button labeled '2'
:block.display_box := :block.display_box || '2';