我基本上想从我的硬盘读取一个文本文件,然后使用冒泡排序对文本文件中的字符串元素进行排序(使用IComparable
界面)。我知道这可能是一个愚蠢的问题,但我被困在这里:下面的代码
using System.IO;
namespace Pratictice
{
public partial class CfrmPractice : Form
{
public CfrmPractice()
{
InitializeComponent();
}
private static T[] BubbleSort<T>(T[] list) where T : IComparable
{
T temp;
bool isSorted = false;
while (!isSorted)
{
isSorted = true;
for (int i = 0; i < list.Length - 1; i++)
{
if (list[i].CompareTo(list[i + 1]) > 0)
{
temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
isSorted = false;
return (T) Convert.ChangeType(isSorted[bool],typeof(T));
}
}
}
}
private void btnSort_Click(object sender, EventArgs e)
{
}
public void LoadDataSource()
{
ofdLoadData.InitialDirectory = Application.StartupPath;
//ofdLoadData.FileName = "Text FIles (.txt)|*.txt|All Files (*.*)|*.*|";
if (ofdLoadData.ShowDialog() == DialogResult.OK)
{
try
{
using (StreamReader f = new StreamReader(ofdLoadData.FileName))
{
string sLine;
string[] Fields;
while (!f.EndOfStream)
{
sLine = f.ReadLine();
Fields = sLine.Split(' ');
lstLoadData.Items.Add(Fields[0]);
}
}
}
catch (Exception error)
{
MessageBox.Show(error.Message, "Practice");
}
}
}
private void LoadData_Click(object sender, EventArgs e)
{
LoadDataSource();
}
private void lstLoadData_SelectedIndexChanged(object sender, EventArgs e)
{
}
class Compare : IComparable
{
public string FileName;
public int CompareTo(object obj)
{
if (this.FileName == ((Compare)obj).FileName)
return 0;
else if (this.FileName != ((Compare)obj).FileName)
return 1;
else
return -1;
}
}
}
}