.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Sort <T> List

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
Dexterel
564 Views, 3 Replies

Sort <T> List

I made a list of BaseLines form multiple corridors and I need to sort this list by Alignment and Profile.

So all BaseLines with the same Alignment and Profile to be consecutive in my list.

Is the sorting correct?

 

                    List<Baseline> blList = new List<Baseline>();
                    foreach (Corridor Cor in multipleCorridors)
                    {
                        for (int i = 0; i < Cor.Baselines.Count; i++)
                        {
                            blList.Add(Cor.Baselines[i]);
                        }
                    }

                    blList.Sort(delegate (Baseline x, Baseline y)
                    {
                        int a;
                        a = x.AlignmentId.CompareTo(y.AlignmentId);
                        if (a == 0)
                        {
                            a = x.ProfileId.CompareTo(y.ProfileId);
                        }
                        return a;
                    });

3 REPLIES 3
Message 2 of 4
_gile
in reply to: Dexterel

Hi,

 

Your sorting seems correct, but you can also use the IEnumerable.OrderBy() and IEnumerable.ThenBy() Linq extension methods.

 

blList = blList.OrderBy(cor => cor.AlignmentId).ThenBy(cor => cor.ProfileId).ToList();


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 4
Dexterel
in reply to: _gile

I'm a noob.
On my list I only see Sort(). I have no idea how to install Linq extension.
After installing Linq extension I will see method OrderBy() and ThenBy() on my list?
Message 4 of 4
Dexterel
in reply to: Dexterel

I have succeeded and Linq works now, thank you

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report