C#可排序的集合,它允许重复键

2023-09-02 02:01:20 作者:她似梦却是命

我写一个程序来设置序列中,不同的对象会出现在报告中。 顺序是Excel的小号preadsheet Y位置(小区)。

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.

的code的演示部分如下。 我想做到的是有一个集合,这将让我添加多个对象,并根据该序列,我可以得到一个分类收集

A demo part of code is below. What I want to accomplish is to have a collection, which will allow me to add multiple objects and I can get a sorted collection based on the sequence

SortedList list = new SortedList();

Header h = new Header();
h.XPos = 1;
h.name = "Header_1";
list.Add(h.XPos, h);

h = new Header();
h.XPos = 1;
h.name = "Header_2";
list.Add(h.XPos, h);

我知道排序列表不会允许这一点,我一直在寻找替代。我不想为消除重复的和已经尝试过名单,其中,KeyValuePair< INT,对象>>

I know that the SortedList will not allow this and I have been searching for alternate. I don't want to eliminate the duplicates and already tried List<KeyValuePair<int, object>>.

感谢。

推荐答案

非常感谢您的帮助。在寻找多了,我发现这个解决方案。 (可在Stackoverflow.com其它问题)

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

那么这个类应该持有的对象,每个对象的POSX去为INT位置

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

List<MyPosition> Sequence= new List<MyPosition>();
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.

相关推荐