Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我将如何为这个2D字符串数组制作一个按字母顺序排序的Bubblesort?_C#_Arrays_Sorting - Fatal编程技术网

C# 我将如何为这个2D字符串数组制作一个按字母顺序排序的Bubblesort?

C# 我将如何为这个2D字符串数组制作一个按字母顺序排序的Bubblesort?,c#,arrays,sorting,C#,Arrays,Sorting,我正在尝试为我目前正在做的一个小DB项目学习不同的排序方法。更具体地说,我想了解如何为2D字符串数组实现按字母顺序排序的Bubblesort,因为我发现的大多数示例都是一维整数数组,并且涉及按长度/大小排序的过程 我的数组如下所示: public static string[,] playerArray = new string[20, 8]; 玩家存储在阵列中的方式如下: playerArray[currentIndexPlayer, 0] = playerFirstNameBox.Text

我正在尝试为我目前正在做的一个小DB项目学习不同的排序方法。更具体地说,我想了解如何为2D字符串数组实现按字母顺序排序的Bubblesort,因为我发现的大多数示例都是一维整数数组,并且涉及按长度/大小排序的过程

我的数组如下所示:

public static string[,] playerArray = new string[20, 8];
玩家存储在阵列中的方式如下:

playerArray[currentIndexPlayer, 0] = playerFirstNameBox.Text;
playerArray[currentIndexPlayer, 1] = playerIgNameBox.Text;
playerArray[currentIndexPlayer, 2] = playerSecondNameBox.Text;
playerArray[currentIndexPlayer, 3] = contactStreetBox.Text;
playerArray[currentIndexPlayer, 4] = contactTownBox.Text;
playerArray[currentIndexPlayer, 5] = contactPostcodeBox.Text;
playerArray[currentIndexPlayer, 6] = contactEmailBox.Text;
playerArray[currentIndexPlayer, 7] = contactTelephoneBox.Text;
我只想按playerIgName排序

谢谢你的帮助和指点

完整代码:

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;
using System.Collections;
using System.Text.RegularExpressions;
using System.Runtime.Serialization.Formatters.Binary;

namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
    //[Serializable]
    public static string[,] playerArray = new string[20, 8]; // Initial player array is declared, this is where the players are stored//


    int currentIndexPlayer = 0;
    int currentIndex = 0;
    bool pastFirstZero = false;
    private string filePathName = @"C:\test.txt";
    ImageList picList = new ImageList();
    private ArrayList matches = new ArrayList(4);
    private ArrayList matchProperties = new ArrayList(6);
    string[,] sortedPlayer = new string[playerArray.GetUpperBound(0), playerArray.GetUpperBound(1)];

    public Form1()

    {
        InitializeComponent();
        positionLabel.Text = "Current position:" + (currentIndex + 1).ToString() + "/20";
        debugLabel.Text = currentIndex.ToString();
        clearAllFields();
        LoadArray(filePathName, playerArray);
        matches = new ArrayList();
        matchProperties = new ArrayList();
        SaveArray(filePathName, playerArray);
        LoadArray(filePathName, playerArray);
        string pFirstName = playerFirstNameBox.Text;

        for (int i = 0; i < playerArray.GetUpperBound(0); i++)
        {
            for (int j = 0; j < playerArray.GetUpperBound(1); j++)
            {

                playerArray[i, j] = "";
            }
        }
        LoadArrayAtStart();
    }
    private void LoadArrayAtStart ()
    {
        playerFirstNameBox.Text = playerArray[currentIndex, 0];
        playerIgNameBox.Text = playerArray[currentIndex, 1];
        playerSecondNameBox.Text = playerArray[currentIndex, 2];
        contactStreetBox.Text = playerArray[currentIndex, 3];
        contactTownBox.Text = playerArray[currentIndex, 4];
        contactPostcodeBox.Text = playerArray[currentIndex, 5];
        contactEmailBox.Text = playerArray[currentIndex, 6];
        contactTelephoneBox.Text = playerArray[currentIndex, 7];
    }
    private void LoadSortedArray()
    {
        playerFirstNameBox.Text = sortedPlayer[currentIndex, 0];
        playerIgNameBox.Text = sortedPlayer[currentIndex, 1];
        playerSecondNameBox.Text = sortedPlayer[currentIndex, 2];
        contactStreetBox.Text = sortedPlayer[currentIndex, 3];
        contactTownBox.Text = sortedPlayer[currentIndex, 4];
        contactPostcodeBox.Text = sortedPlayer[currentIndex, 5];
        contactEmailBox.Text = sortedPlayer[currentIndex, 6];
        contactTelephoneBox.Text = sortedPlayer[currentIndex, 7];
    }
    static void SaveArray(string fileName,  string[,] arr)
    {
        StreamWriter writer = new StreamWriter(fileName);

        try 
        {
            foreach (string entry in playerArray)
            {
                writer.WriteLine(entry);
            }
        }
        catch
        {
            MessageBox.Show("Couldn't save");
            return;
        }
        writer.Close();
    } // End of SaveArray function

    static void LoadArray(string fileName, string[,] arr)
    {
        StreamReader reader = new StreamReader(fileName);
        // First try to open the file for reading

        {
            MessageBox.Show("Error when reading file" +fileName);
            // Didn't work, exit method empty-handed.
        }


        // Now try to actually read the file content
        // This is the tricky bit. 
        // Note the loop-in-loop structure (i.e. a nested 'for' loop):
        try
        {
            for(int i=0; i<=arr.GetUpperBound(0); ++i)
            {
                for (int j = 0; j<=arr.GetUpperBound(1); ++j)
                {
                    arr[i, j] = reader.ReadLine();
                    //Console.WriteLine(arr[i, j]);
                }
            }

        }

        catch
        {
            MessageBox.Show("Could not read from file " +fileName);
        }

        // Important: close the file again
        reader.Close();

    } // End of ReadArray function


    public void clearAllFields()
    {
        playerFirstNameBox.Clear();
        playerIgNameBox.Clear();
        playerSecondNameBox.Clear();
        contactStreetBox.Clear();
        contactTownBox.Clear();
        contactPostcodeBox.Clear();
        contactEmailBox.Clear();
        contactTelephoneBox.Clear();
    }

    private void addNewPlayerBtn_Click(object sender, EventArgs e)
    {
        if (addNewPlayerBtn.Text == "Add New Player")
        {
            if (pastFirstZero == true)
            {
                currentIndex++;
            }
            if (currentIndex == 0)
            {
                pastFirstZero = true;
            }

            playerFirstNameBox.ReadOnly = false; //Unlock all fields for input//
            playerIgNameBox.ReadOnly = false;
            playerSecondNameBox.ReadOnly = false;
            contactStreetBox.ReadOnly = false;
            contactTownBox.ReadOnly = false;
            contactPostcodeBox.ReadOnly = false;
            contactEmailBox.ReadOnly = false;
            contactTelephoneBox.ReadOnly = false;

            clearAllFields();

            positionLabel.Text = "Current position:" + (currentIndex + 1).ToString() + "/20";
            debugLabel.Text = currentIndex.ToString();
            addNewPlayerBtn.Text = "Confirm";
        }


        else if (addNewPlayerBtn.Text == "Confirm")
        {
            if ((playerFirstNameBox.Text != "") && (playerIgNameBox.Text != "") && (playerSecondNameBox.Text != "") && (currentIndexPlayer < 20)) // Validation check to see if all main fields were entered
            {
                string playerFirstName = playerFirstNameBox.Text;
                string playerIGName = playerFirstNameBox.Text;
                string playerSecondName = playerSecondNameBox.Text;

                for (int i = 0; i < playerArray.GetUpperBound(0); i++)
                {
                    for (int j = 0; j < playerArray.GetUpperBound(1); j++)
                    {
                        if (playerArray[i, j].Equals(playerFirstName) && (playerArray[i, j].Equals(playerIGName) && playerArray[i, j].Equals(playerSecondName)))
                        {
                            MessageBox.Show("Player Duplicate!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); //Validation failed, display error message//
                            clearAllFields();
                            return;
                        }
                    }
                }
                playerArray[currentIndexPlayer, 0] = playerFirstNameBox.Text;
                playerArray[currentIndexPlayer, 1] = playerIgNameBox.Text;
                playerArray[currentIndexPlayer, 2] = playerSecondNameBox.Text;
                playerArray[currentIndexPlayer, 3] = contactStreetBox.Text;
                playerArray[currentIndexPlayer, 4] = contactTownBox.Text;
                playerArray[currentIndexPlayer, 5] = contactPostcodeBox.Text;
                playerArray[currentIndexPlayer, 6] = contactEmailBox.Text;
                playerArray[currentIndexPlayer, 7] = contactTelephoneBox.Text;



                if (currentIndexPlayer < 20)
                {
                    currentIndexPlayer++;
                }
                debugLabel.Text = currentIndex.ToString();



            }
            else
            {
                if ((playerFirstNameBox.Text != "") && (playerIgNameBox.Text != "") && (playerSecondNameBox.Text != ""))
                {
                    MessageBox.Show("Database full!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); //Validation failed, display error message//
                    addNewPlayerBtn.Text = "Confirm";
                }
                else
                {
                    MessageBox.Show("Fields missing!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); //Validation failed, display error message//
                    addNewPlayerBtn.Text = "Confirm";
                }

            }

            addNewPlayerBtn.Text = "Add New Player";

            playerFirstNameBox.ReadOnly = true; //lock all fields for input again//
            playerIgNameBox.ReadOnly = true;
            playerSecondNameBox.ReadOnly = true;
            contactStreetBox.ReadOnly = true;
            contactTownBox.ReadOnly = true;
            contactPostcodeBox.ReadOnly = true;
            contactEmailBox.ReadOnly = true;
            contactTelephoneBox.ReadOnly = true;
            SaveArray(filePathName, playerArray);

        }
    }

    private void searchToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Form2 searchForm = new Form2();
        searchForm.Show();
    }

    private void showPreviousBtn_Click(object sender, EventArgs e)
    {
        if (currentIndex == 0)
        {
            currentIndex = 19;
        }
        else
        {
            currentIndex--;
        }
        playerFirstNameBox.Text = playerArray[currentIndex, 0];
        playerIgNameBox.Text = playerArray[currentIndex, 1];
        playerSecondNameBox.Text = playerArray[currentIndex, 2];
        contactStreetBox.Text = playerArray[currentIndex, 3];
        contactTownBox.Text = playerArray[currentIndex, 4];
        contactPostcodeBox.Text = playerArray[currentIndex, 5];
        contactEmailBox.Text = playerArray[currentIndex, 6];
        contactTelephoneBox.Text = playerArray[currentIndex, 7];

        positionLabel.Text = "Current position:" + (currentIndex + 1).ToString() + "/20";
        debugLabel.Text = currentIndex.ToString();

    }

    private void showNextBtn_Click(object sender, EventArgs e)
    {
        if (currentIndex == 19)
        {
            currentIndex = 0;
        }
        else
        {
            currentIndex++;

        }

        playerFirstNameBox.Text = playerArray[currentIndex, 0];
        playerIgNameBox.Text = playerArray[currentIndex, 1];
        playerSecondNameBox.Text = playerArray[currentIndex, 2];
        contactStreetBox.Text = playerArray[currentIndex, 3];
        contactTownBox.Text = playerArray[currentIndex, 4];
        contactPostcodeBox.Text = playerArray[currentIndex, 5];
        contactEmailBox.Text = playerArray[currentIndex, 6];
        contactTelephoneBox.Text = playerArray[currentIndex, 7];

        positionLabel.Text = "Current position:" + (currentIndex + 1).ToString() + "/20";
        debugLabel.Text = currentIndex.ToString();

    }

    private void deletePlayerBtn_Click(object sender, EventArgs e)
    {
        playerArray[currentIndex, 0] = null;
        playerArray[currentIndex, 1] = null;
        playerArray[currentIndex, 2] = null;
        playerArray[currentIndex, 3] = null;
        playerArray[currentIndex, 4] = null;
        playerArray[currentIndex, 5] = null;
        playerArray[currentIndex, 6] = null;
        playerArray[currentIndex, 7] = null;

        playerFirstNameBox.Text = playerArray[currentIndex, 0];
        playerIgNameBox.Text = playerArray[currentIndex, 1];
        playerSecondNameBox.Text = playerArray[currentIndex, 2];
        contactStreetBox.Text = playerArray[currentIndex, 3];
        contactTownBox.Text = playerArray[currentIndex, 4];
        contactPostcodeBox.Text = playerArray[currentIndex, 5];
        contactEmailBox.Text = playerArray[currentIndex, 6];
        contactTelephoneBox.Text = playerArray[currentIndex, 7];

    }

    private void uploadButton_Click(object sender, EventArgs e)
    {
        try
        {
            OpenFileDialog OpenFd = new OpenFileDialog();
            OpenFd.Filter = "Images only. |*.jpeg; *.jpg; *.png; *.gif;";
            DialogResult rd = OpenFd.ShowDialog();
            if (rd == System.Windows.Forms.DialogResult.OK)
            {
                playerPictureBox.Image = Image.FromFile(OpenFd.FileName);
                // save the FileName to a string in your array
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex.Message);
        }


    }
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.IO;
使用系统集合;
使用System.Text.RegularExpressions;
使用System.Runtime.Serialization.Formatters.Binary;
命名空间Windows窗体应用程序5
{
公共部分类Form1:Form
{
//[可序列化]
public static string[,]playerArray=新字符串[20,8];//声明初始播放器数组,这是存储播放器的地方//
int currentIndexPlayer=0;
int currentIndex=0;
bool pastFirstZero=false;
私有字符串filePathName=@“C:\test.txt”;
ImageList picList=新建ImageList();
私有ArrayList匹配项=新ArrayList(4);
私有ArrayList matchProperties=新ArrayList(6);
string[,]sortedPlayer=新字符串[playerArray.GetUpperBound(0),playerArray.GetUpperBound(1)];
公共表格1()
{
初始化组件();
positionLabel.Text=“当前位置:”+(currentIndex+1).ToString()+“/20”;
debugLabel.Text=currentIndex.ToString();
clearAllFields();
LoadArray(文件路径名、playerArray);
匹配项=新的ArrayList();
matchProperties=新的ArrayList();
存储阵列(文件路径名、playerArray);
LoadArray(文件路径名、playerArray);
字符串pFirstName=playerFirstNameBox.Text;
for(int i=0;i对于(int i=0;i而言,基本的Bubblesort算法是相同的。所有的变化都是交换操作

也就是说,对一维整数数组进行排序是:

for (int i = 0; i < a.Length-1; ++i)
{
    for (int j = i; j < a.Length; ++j)
    {
        if (a[i] > a[j])
        {
            // swap a[i] and a[j]
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    }
}
for(int i=0;ia[j])
{
//互换a[i]和a[j]
int temp=a[i];
a[i]=a[j];
a[j]=温度;
}
}
}
如果要对二维数组进行排序,则交换必须交换各个字段:

for (int i = 0; i < a.GetUpperBound(0)-1; ++i)
{
    for (int j = i; j < a.GetUpperBound(0); ++j)
    {
        if (a[i,0] > a[j,0])
        {
            // swap a[i] and a[j]
            for (int x = 0; x < a.GetUpperBound(1); ++x)
            {
                string temp = a[i,x];
                a[i,x] = a[j,x];
                a[j,x] = temp;
            }
        }
    }
}
for(inti=0;ia[j,0])
{
//互换a[i]和a[j]
对于(int x=0;x
您确实需要了解。将播放器数据存储为二维字符串数组是我们在1968年用FORTRAN程序所做的事情。使用一维数组或
播放器
实例列表更容易,更不用说更容易理解了。@Jim Mischel Haha,好笑:)恐怕我的项目要求我使用2D/3D阵列。这篇文章没有解释尝试的步骤、遇到的失败以及具体的预期行为。因此,我建议关闭这个问题。如果你选择编辑这篇文章以包含此内容,我将撤回我的建议。