C# Sortable 集合,允许重复键 [英] C# Sortable collection which allows duplicate keys

查看:76
本文介绍了C# Sortable 集合,允许重复键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序来设置各种对象将出现在报告中的顺序.序列是 Excel 电子表格上的 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.

代码的演示部分如下.我想要完成的是有一个集合,这将允许我添加多个对象,我可以根据序列获得一个排序的集合

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

我知道 SortedList 不允许这样做,我一直在寻找替代方法.我不想消除重复项并且已经尝试过List>.

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 Position

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.

这篇关于C# Sortable 集合,允许重复键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆