0

我基本上想从我的硬盘读取一个文本文件,然后使用冒泡排序对文本文件中的字符串元素进行排序(使用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;
            }
        }

    }
} 
4

1 回答 1

1

好吧,我看到一些时髦的事情发生了。我不明白为什么在修改传递给方法的列表时需要返回类型。我也不明白你想用 Convert.ChangeType 实现什么。这对你有用吗?

private static void 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;
            }
        }
    }
}
于 2013-04-03T22:50:55.983 回答