如何将文本文件中不同类型的数字按降序排序到文本框?
How to sort different type numbers which with are in text file to textbox in descending?
它应该让我从我的电脑中选择一个文本文件。这个文本文件中会有数字。这些数字将由“white-space”字符分隔(提示:确保您考虑了所有的 white-space 字符,否则您的程序可能会失败)。十进制数字的小数位将以逗号分隔。文本文件中的数字数量是不确定的。当我 select 文件并单击按钮时,我应该看到文本文件中的所有数字按从大到小的顺序排序。
示例文本文件的内容如下所示:
56 45 6 2 789
9 349 -87
11
4,34 -198,456
65
9,85 45
-1
99,456
877 56 34 4
我在 Visual Studio 2017 年工作。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace TestUygulamasi {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) //get file and paste to testbox
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK) {
try {
if ((myStream = openFileDialog1.OpenFile()) != null) {
using (myStream) {
textBox1.Text = File.ReadAllText(openFileDialog1.FileName);
}
}
} catch (Exception ex) {
MessageBox.Show("Error: File has not read. Error: " + ex.Message);
}
}
}
private void button2_Click(object sender, EventArgs e)//Sorting Button
{
string numbers = textBox1.Text;
string[] arr;
arr = numbers.Split(' ');
Array.Sort(arr);
string sorting = int.Parse(textBox2.Text);
for (int i = 0; i < arr.Length; ++i) {
sorting = sorting + arr[i] + "\n";
//sorting = arr[i] + "\n";
}
textBox2.Text = Convert.ToString(sorting);
//listBox1.Items.Add("\n" + sorting + "\n");
//MessageBox.Show(sorting);
}
}
}
我无法对数字进行降序排列。
您可以为此目的使用 Linq。
var str = textBox1.Text;
var currentCulture = (CultureInfo)CultureInfo.InvariantCulture.Clone();
currentCulture.NumberFormat.NumberDecimalSeparator = ",";
currentCulture.NumberFormat.NumberGroupSeparator = ".";
var numbers = str.Split(new string[] { " ",Environment.NewLine },StringSplitOptions.RemoveEmptyEntries)
.Select(num=> decimal.Parse(num,currentCulture))
.OrderBy(num=>num);
textBox1.Text = string.Join(Environment.NewLine,numbers);
另一个答案是这样的。我没有写这段代码。编写代码的人已删除代码和 his/her 评论。为什么 he/she 做到了?我不知道。希望he/she再回答下这个问题。我是该网站的新手,所以我无法投票 his/her 答案。谢谢我失去的秘密英雄
List<decimal> nums = new List<decimal>();
for (int i = 0; i < textBox1.Lines.Count(); ++i)
{
string[] line = textBox1.Lines[i].Split(new char[] { ' ' },
StringSplitOptions.RemoveEmptyEntries);
foreach (string s in line)
{
nums.Add(decimal.Parse(s, NumberStyles.Number, new CultureInfo("tr-TR")));
}
}
foreach (decimal d in nums.OrderByDescending(x => x))
{
listBox1.Items.Add(d.ToString());
}
它应该让我从我的电脑中选择一个文本文件。这个文本文件中会有数字。这些数字将由“white-space”字符分隔(提示:确保您考虑了所有的 white-space 字符,否则您的程序可能会失败)。十进制数字的小数位将以逗号分隔。文本文件中的数字数量是不确定的。当我 select 文件并单击按钮时,我应该看到文本文件中的所有数字按从大到小的顺序排序。
示例文本文件的内容如下所示:
56 45 6 2 789
9 349 -87
11
4,34 -198,456
65
9,85 45
-1
99,456
877 56 34 4
我在 Visual Studio 2017 年工作。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace TestUygulamasi {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) //get file and paste to testbox
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK) {
try {
if ((myStream = openFileDialog1.OpenFile()) != null) {
using (myStream) {
textBox1.Text = File.ReadAllText(openFileDialog1.FileName);
}
}
} catch (Exception ex) {
MessageBox.Show("Error: File has not read. Error: " + ex.Message);
}
}
}
private void button2_Click(object sender, EventArgs e)//Sorting Button
{
string numbers = textBox1.Text;
string[] arr;
arr = numbers.Split(' ');
Array.Sort(arr);
string sorting = int.Parse(textBox2.Text);
for (int i = 0; i < arr.Length; ++i) {
sorting = sorting + arr[i] + "\n";
//sorting = arr[i] + "\n";
}
textBox2.Text = Convert.ToString(sorting);
//listBox1.Items.Add("\n" + sorting + "\n");
//MessageBox.Show(sorting);
}
}
}
我无法对数字进行降序排列。
您可以为此目的使用 Linq。
var str = textBox1.Text;
var currentCulture = (CultureInfo)CultureInfo.InvariantCulture.Clone();
currentCulture.NumberFormat.NumberDecimalSeparator = ",";
currentCulture.NumberFormat.NumberGroupSeparator = ".";
var numbers = str.Split(new string[] { " ",Environment.NewLine },StringSplitOptions.RemoveEmptyEntries)
.Select(num=> decimal.Parse(num,currentCulture))
.OrderBy(num=>num);
textBox1.Text = string.Join(Environment.NewLine,numbers);
另一个答案是这样的。我没有写这段代码。编写代码的人已删除代码和 his/her 评论。为什么 he/she 做到了?我不知道。希望he/she再回答下这个问题。我是该网站的新手,所以我无法投票 his/her 答案。谢谢我失去的秘密英雄
List<decimal> nums = new List<decimal>();
for (int i = 0; i < textBox1.Lines.Count(); ++i)
{
string[] line = textBox1.Lines[i].Split(new char[] { ' ' },
StringSplitOptions.RemoveEmptyEntries);
foreach (string s in line)
{
nums.Add(decimal.Parse(s, NumberStyles.Number, new CultureInfo("tr-TR")));
}
}
foreach (decimal d in nums.OrderByDescending(x => x))
{
listBox1.Items.Add(d.ToString());
}