使用冒泡排序和IComparable接口对文本文件进行排序

本文关键字:文件 文本 排序 接口 冒泡排序 IComparable | 更新日期: 2023-09-27 18:07:14

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

使用冒泡排序和IComparable接口对文本文件进行排序

我看到一些时髦的东西在发生。我不明白为什么在修改传递给方法的列表时需要返回类型。我也不明白你想用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;
            }
        }
    }
}