我正在尝试为我正在做的一个小型数据库项目学习不同的排序方法。更具体地说,我想了解如何为2D字符串数组实现Bubblesort按字母顺序排序,因为我发现的大多数示例都是单维整数数组,并涉及按长度/大小排序的过程。
My Array看起来像这样:
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);
}
}
答案 0 :(得分:0)
基本的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; 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;
}
}
}
}