Is there a list that is sorted automatically in .NET?

前端 未结 8 1180
轻奢々
轻奢々 2021-02-05 04:00

I have a collection of Layers where they have names and colors. What I want to do is to sort these first based on colors, then based on their names:



        
8条回答
  •  逝去的感伤
    2021-02-05 04:27

    You can use arraylist and do below linq query to sort them

    ArrayList myList = new ArrayList();
                Layer obj1 = new Layer();
                obj1.Color = LayerColor.Red;
                obj1.Name = "Layer1";
                myList.Add(obj1);
    
                Layer obj2 = new Layer();
                obj2.Color = LayerColor.Green;
                obj2.Name = "Layer2";
                myList.Add(obj2);
    
                Layer obj3 = new Layer();
                obj3.Color = LayerColor.Blue;
                obj3.Name = "Layer3";
                myList.Add(obj3);
    
                Layer obj4 = new Layer();
                obj4.Color = LayerColor.Green;
                obj4.Name = "Layer4";
                myList.Add(obj4);
    
    
                var mySortedList = myList.OfType().OrderBy(l => l.Color)
                             .ThenBy(l => l.Name);
    

提交回复
热议问题