0

I am currently developing a phone book in our Intranet (ASP.NET) that pulls out information from our Active Directory and stores them in a List

We have several branches in the country, and i need the employees to be shown grouped by branch, which i did using the following:

lstADUsers.Sort(delegate (MyUser x, MyUser y)
{
  return x.Branch.CompareTo(y.Branch);
});

The problem is that i also want to apply a second sort, so employees are shown ordered by branch name and then in order of relevance (job position). So i want to show vicepresidents first, then managers, then engineers and such... I currently don't have that sort order stored anywhere, so i though of assigning each job position some kind of weight or priority and store it somewhere (database? map?)

I would appreciate any help in this matter. Thanks

EDIT: i would like an efficient method to store the weight of the second property. Right now i solved this by creating a property type int and then assigning weight manually with the following code:

if (objUsuarios.Cargo.Contains("something"))
{
    objUsuarios.OrdenCargo = 1;
}
else if (objUsuarios.Cargo.Contains("another thing"))
{
    objUsuarios.OrdenCargo = 2;
}

and in my delegate i am using a triple sort, first by branch, then by job, then by employee name:

lstADUsers.Sort(delegate (Usuario x, Usuario y)
{
    int valor = x.Piso.CompareTo(y.Piso);
    if (valor != 0)
        return x.Piso.CompareTo(y.Piso);
    else
    { 
        int valorordencargo = x.OrdenCargo.CompareTo(y.OrdenCargo);
        if (valorordencargo != 0)                          
            return x.OrdenCargo.CompareTo(y.OrdenCargo);
        else
        {
            int valornombrecargo = x.Cargo.CompareTo(y.Cargo);
            if (valornombrecargo != 0)
                return x.Cargo.CompareTo(y.Cargo);
            else
            {
                return x.Nombre.CompareTo(y.Nombre);
            }
        }
    }
});
fjleon
  • 341
  • 1
  • 3
  • 10
  • I've seen the other question and and I'm not sure this is a duplicate, so I give a hint to fjelon: You can use the ICollectionView interface and the CollectionViewSource to create a Sorted View from your collection using different sort criteria. Let me know if you need some code I can't write it here in comments. – Sabrina_cs Sep 23 '15 at 13:28
  • for what i have read in other solutions, LINQ is easy but inefficient for this task, also since the second property needs a custom method to decide the sorting (as shown in the edit above) i don't think i can use it. – fjleon Sep 23 '15 at 15:15
  • sabrina_cs: i already have a working solution (see the edit), now i am looking for optimization, especially when assigning the weight to the different job positions in the company. Right know i have 12 if-else if and counting – fjleon Sep 23 '15 at 15:17

0 Answers0