C# Sortable collection which allows duplicate keys

前端 未结 16 2140
旧巷少年郎
旧巷少年郎 2020-11-28 10:06

I am writing a program to set a sequence in which various objects will appear in report. The sequence is the Y position (cell) on Excel spreadsheet.

A demo part of co

16条回答
  •  眼角桃花
    2020-11-28 10:57

    Thanks a lot for your help. While searching more, I found this solution. (Available in Stackoverflow.com in other question)

    First, I created a class which would encapsulate my objects for classes (Headers,Footer etc)

    public class MyPosition
    {
        public int Position { get; set; }
        public object MyObjects{ get; set; }
    }
    

    So this class is supposed to hold on the objects, and PosX of each object goes as int Position

    List Sequence= new List();
    Sequence.Add(new MyPosition() { Position = 1, Headerobject });
    Sequence.Add(new MyPosition() { Position = 2, Headerobject1 });
    Sequence.Add(new MyPosition() { Position = 1, Footer });
    
    League.Sort((PosA, PosB) => PosA.Position.CompareTo(PosB.Position));
    

    What eventually I get is the sorted "Sequence" list.

提交回复
热议问题