Click here to Skip to main content
15,885,915 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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!

Can anyone help me please?

Thank you
Posted
Updated 23-Jan-13 9:16am
v2
Comments
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

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();
 
Share this answer
 
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
    orderby e.Code, e.Name, e.Address, e.Phone
    select e;

Employer[] sortedEmployers = q.ToArray();
 
Share this answer
 
v4
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
 
Share this answer
 
Comments
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! :)
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..
 
Share this answer
 
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();

ReadEmployers();
}



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);
LEmp.Add(V);
listBox1.Items.Add(V.getName());
listBox1.Show();




}
else if (cb_tipo.Text == "Admin")
{


Admin Adm = new Admin(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);

LEmp.Add(Adm);
listBox1.Items.Add(Adm.getNome());
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);

LEmp.Add(T);
listBox1.Items.Add(T.getName());
listBox1.Show();


}

SaveEmployers();
}
}

private void bttonClearFIelds_Click(object sender, EventArgs e)
{
cb_tipo.Text = "";
tb_BI.Text = "";
tb_Nome.Text = "";
cb_Sexo.Text = "";
tb_Morada.Text = "";
tb_Morada.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_Adress.Text = LEmp[listBox1.SelectedIndex].getAdress();
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()
{
string gravar = "DadosFuncionarios.txt";
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();
}

public void ReadEmployer()
{
string ler = "DadosFuncionarios.txt";
if (!File.Exists(ler))
return;
StreamReader txtln = new StreamReader(new FileStream(ler, FileMode.Open, FileAccess.Read));
LFunc.Clear();
listBox1.Items.Clear();
while (txtln.Peek() != -1)
{
string linha = txtln.ReadLine();
string[] campos = linha.Split('|');


Funcionario Func;

if (campos[0] == "Administrativo")
{
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);
}
else if (campos[0] == "Tratador")
{
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);
}
LFunc.Add(Func);
listBox1.Items.Add(Func.getNome());
}

txtln.Close();
}




}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900