14,920,001 members
See more:
Hello, im doing a project for school.

I have a list with employers and everything is work fine, but now i have to sort the items of the list using the code of employer.

I need to use one method like shellsort, bubblesort, direct insertion.
I already saw the code of each method(shellsort,bubblesort,direct insertion), but i dont understand how i sort :/

Can you explain me how i convert a list to a array?

But i think that my teacher want the sort using a sort method!

Thank you
Posted
Updated 23-Jan-13 9:16am
v2
joshrduncan2012 23-Jan-13 15:06pm

My suggestion would be to convert the list to an array and use the Array.Sort command.
mibetty 23-Jan-13 15:34pm

Employer[] arrayFunc = LEmp.ToArray();
Array.Sort(arrayFunc);
List<funcionario> sorted = arrayFunc.ToList();
listBox1.Show();

I did this but i get the same error : Failed to compare two elements of the matrix
Andreas Gieriet 23-Jan-13 15:46pm

You don't tell how to sort, i.e. based on what criterion (Code of the Employer class).
See solution #2, #4, and my #5 on how to tell that.
Cheers
Andi

## Solution 2

Easiest way? Don't write your own! :laugh:

C#
List<Employer> myList = new List<Employer>();
//Fill the list
myList.Sort((x, y) => Comparer.Default.Compare(x.Code, y.Code));

Or, if you want to preserve the order of the original list:
C#
Employer[] array = myList.ToArray();
Array.Sort(array);
List<Employer> sorted = array.ToList();

## Solution 4

OriginalGriff has the right idea, don't roll your own use the .Net Framework.

You could also write it this way:
C#
Employer.Sort((x,y)=>x.Code.CompareTo(y.Code));

....Or use Linq:
C#
var q = from e in Employer
select e;

Employer[] sortedEmployers = q.ToArray();
v4

## Solution 5

If LINQ is allowed and you want to get a new sorted list, you may use either of the variants below:
C#
List<Employer> sortedList = (from e in list orderby e.Code select e).ToList();

C#
List<Employer> sortedList = list.OrderBy(e => e.Code).ToList();

Cheers
Andi
mibetty 23-Jan-13 16:23pm

Thanks for help but i cant use Linq, just bubblesort,direct selection, direct insert, shellsort.
Thanks anyway :)
Andreas Gieriet 23-Jan-13 17:56pm

You may want to checkout Sorting Algorithms In C#. See the associated code for download.
Cheers
Andi
mibetty 23-Jan-13 20:02pm

Thank you very much, i will study that! :)

## Solution 3

i created a button and i did this :
C#
LFunc.ToArray();
LFunc.Sort()

but i got an error : Failed to compare two elements of the matrix

but also, doing this i dont sort the list by code.

Each employer have 7 fields, code, name, adress,phone,etc.

and i need to order in a listbox the employers by code..

## Solution 6

Im trying to do the sort of the employer list but until now i can´t do it !

Can you Please seeing the code and try to find the better way to sort my list?

Sorry for putting all this code but i dont know what to do.

Very Thanks!!

public partial class FormEmployer : Form
{
List<employer> LEmp = new List<employer>();

public FormEmployer()
{
InitializeComponent();

}

private void bttonRemove_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex == -1)
return;

LEmp.Remove(LFunc[listBox1.SelectedIndex]);
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
SaveEmployer();

}

private void bttonAdd_Click(object sender, EventArgs e)
{

if (cb_type.Text == "")
{
MessageBox.Show("Please put the type of employer", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}

else if (cb_type.Text == "" || tb_code.Text == "" || tb_Name.Text == "" || dateTimePicker1.Text == "" || cb_Sex.Text == "" || tb_Adress.Text == "" || tb_tphone.Text == "" || tb_Email.Text == "")
{
MessageBox.Show("Fields empty", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}

else if (cb_type.Text == "Vet")
{

Vet V = new Vet(Convert.ToInt32(tb_Code.Text), tb_Name.Text, Convert.ToDateTime(dateTimePicker1.Text), cb_Sex.Text, tb_Adress.Text, Convert.ToInt32(tb_phone.Text), tb_Email.Text, pictureBox1.Image);
listBox1.Show();

}
{

listBox1.Show();

}
else if (cb_type.Text == "Treater")
{

Treater T = new Treater(Convert.ToInt32(tb_Code.Text), tb_Name.Text, Convert.ToDateTime(dateTimePicker1.Text), cb_Sex.Text, tb_Adress.Text, Convert.ToInt32(tb_phone.Text), tb_Email.Text, pictureBox1.Image);

listBox1.Show();

}

SaveEmployers();
}
}

private void bttonClearFIelds_Click(object sender, EventArgs e)
{
cb_tipo.Text = "";
tb_BI.Text = "";
tb_Nome.Text = "";
cb_Sexo.Text = "";
tb_telefone.Text = "";
pictureBox1.Image = pictureBox1.InitialImage;
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{

if (listBox1.SelectedIndex == -1)
return;

cb_type.Text = LEmp[listBox1.SelectedIndex].getType();
tb_COde.Text = LEmp[listBox1.SelectedIndex].getBi().ToString();
tb_Name.Text = LEmp[listBox1.SelectedIndex].getName();
dateTimePicker1.Text=LEmp[listBox1.SelectedIndex].getBorn().ToString();

cb_Sex.Text = LEmp[listBox1.SelectedIndex].getSex();
tb_Phone.Text = LEmp[listBox1.SelectedIndex].getPhone().ToString();
tb_Email.Text = LEmp[listBox1.SelectedIndex].getEmail();
pictureBox1.Image = LEmp[listBox1.SelectedIndex].getPhoto();

}

private void bttonAddPhoto_Click_1(object sender, EventArgs e)
{
OpenFileDialog importarImagem = new OpenFileDialog();
importarImagem.Filter = "Jpg files(*.jpg) | *.jpg";
importarImagem.InitialDirectory = @"C:\";

if (importarImagem.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(importarImagem.FileName);
}

}

public void SaveEmployer()
{
StreamWriter textOut = new StreamWriter(new FileStream(gravar, FileMode.Create, FileAccess.Write));

foreach (Funcionario F in LFunc)
{
F.GravarFicheiro(textOut);

}
textOut.Close();
}

private void FormFuncionario_Deactivate(object sender, EventArgs e)
{
GravarFuncionario();
}

{
if (!File.Exists(ler))
return;
LFunc.Clear();
listBox1.Items.Clear();
while (txtln.Peek() != -1)
{
string[] campos = linha.Split('|');

Funcionario Func;

{
Func = new Administrativo(Convert.ToInt32(campos[1]), campos[2], Convert.ToDateTime(campos[3]), campos[4], campos[5], Convert.ToInt32(campos[6]), campos[6], pictureBox1.Image);
}
{
Func = new Tratador(Convert.ToInt32(campos[1]), campos[2], Convert.ToDateTime(campos[3]), campos[4], campos[5], Convert.ToInt32(campos[6]), campos[6], pictureBox1.Image);
}
else //if (campos[0] == "Veterinário") não é preciso porque é o que resta basta o else
{
Func = new Veterinario(Convert.ToInt32(campos[1]), campos[2], Convert.ToDateTime(campos[3]), campos[4], campos[5], Convert.ToInt32(campos[6]), campos[6], pictureBox1.Image);
}
}

txtln.Close();
}

}