Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 23-Jan-13 9:05am
mibetty480
Edited 23-Jan-13 9:16am
BC @ CV2.6K
v2
Comments
joshrduncan2012 at 23-Jan-13 15:06pm
   
My suggestion would be to convert the list to an array and use the Array.Sort command.
mibetty at 23-Jan-13 15:34pm
   
Employer[] arrayFunc = LEmp.ToArray();
Array.Sort(arrayFunc);
List sorted = arrayFunc.ToList();
listBox1.Show();
 
I did this but i get the same error : Failed to compare two elements of the matrix
Andreas Gieriet at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Easiest way? Don't write your own! Laugh | :laugh:
 
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:
Employer[] array = myList.ToArray();
Array.Sort(array);
List<Employer> sorted = array.ToList();
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

OriginalGriff has the right idea, don't roll your own use the .Net Framework.
 
You could also write it this way:
Employer.Sort((x,y)=>x.Code.CompareTo(y.Code));
 
....Or use Linq:
var q = from e in Employer
    orderby e.Code, e.Name, e.Address, e.Phone
    select e;
 
Employer[] sortedEmployers = q.ToArray();
  Permalink  
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

If LINQ is allowed and you want to get a new sorted list, you may use either of the variants below:
List<Employer> sortedList = (from e in list orderby e.Code select e).ToList();
List<Employer> sortedList = list.OrderBy(e => e.Code).ToList();
Cheers
Andi
  Permalink  
Comments
mibetty at 23-Jan-13 16:23pm
   
Thanks for help but i cant use Linq, just bubblesort,direct selection, direct insert, shellsort.
Thanks anyway :)
Andreas Gieriet at 23-Jan-13 17:56pm
   
You may want to checkout Sorting Algorithms In C#. See the associated code for download.
Cheers
Andi
mibetty at 23-Jan-13 20:02pm
   
Thank you very much, i will study that! :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

i created a button and i did this :
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..
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

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 LEmp = new List();

 
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();
}
 

 

}
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 640
1 OriginalGriff 431
2 Maciej Los 255
3 PIEBALDconsult 180
4 Bruno Sprecher 170
0 Sergey Alexandrovich Kryukov 11,069
1 OriginalGriff 5,743
2 Maciej Los 5,649
3 CPallini 3,879
4 BillWoodruff 3,584


Advertise | Privacy | Mobile
Web04 | 2.8.141030.1 | Last Updated 23 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100