- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
.NET是否具有针对IList的就地排序功能?我需要对集合本身进行排序,而不是创建新集合。
编辑:我需要进行适当排序的原因是避免重新绘制UI,除非实际更改过。如果只有一项不合适,我不想创建一百个新的自定义控件。
编辑:同样,这是关于就地排序的问题,而不仅仅是一般的排序。
最佳答案
就地Quicksort / InsertionSort / ShellSort实现如何:
public static class InPlaceQuickSort {
private static void Swap<T>(IList<T> set, int left, int right) {
T temp = set[left];
set[left] = set[right];
set[right] = temp;
}
private static Int32 Partition<T>(IList<T> set, int lBound, int rBound)
where T : IComparable<T> {
T pivot = set[rBound];
int left = lBound - 1;
int right = rBound;
while (true) {
while (set[++left].CompareTo(pivot) < 0) ;
while (set[--right].CompareTo(pivot) > 0) if (left == right) break;
if (left >= right) break;
Swap(set, left, right);
}
Swap(set, left, rBound);
return left;
}
private static IList<T> QuickSort<T>(IList<T> set, int lBound, int rBound)
where T : IComparable<T> {
if (lBound >= rBound) return set;
Int32 pivot = Partition(set, lBound, rBound);
QuickSort(set, lBound, pivot - 1);
QuickSort(set, pivot + 1, rBound);
return set;
}
public static IList<T> InsertionSort<T>(this IList<T> set)
where T : IComparable<T> {
for (Int32 index = 1; index < set.Count; index++) {
for (Int32 insertion = index; insertion > 0 && set[insertion - 1].CompareTo(set[insertion]) > 0; insertion--) {
Swap(set, insertion - 1, insertion);
}
}
return set;
}
public static IList<T> ShellSort<T>(this IList<T> set)
where T : IComparable<T> {
Int32 shell = 1;
while (shell < (set.Count / 3)) shell = shell * 3 + 1;
while (shell >= 1) {
for (Int32 index = shell; index < set.Count; index++) {
for (Int32 insertion = index; insertion >= shell && set[insertion - shell].CompareTo(set[insertion]) > 0; insertion -= shell) {
Swap(set, insertion - shell, insertion);
}
}
shell = shell / 3;
}
return set;
}
public static IList<T> QuickSort<T>(this IList<T> set)
where T : IComparable<T> {
return QuickSort<T>(set, 0, set.Count - 1);
}
}
public static void Main() {
List<Int32> numbers = new List<int> { 1, 3, 2, 4, 2 };
foreach (Int32 number in numbers.QuickSort())
Console.WriteLine(number);
Console.ReadLine();
}
关于.net - 是否在适当位置为IList <T>排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19167193/
我正在尝试转换 IList>至 IList ,我的意思是有一个唯一的列表,其中包含第一个的所有元素(对象)。 public IList> PMTs { get
在 C# 中,如果我有一个 IList,但我不知道 IList 中对象的类型,我该如何创建一个 的副本>IList? 情况是这样的: 我有一个 CollectionEditor,它可以修改 IList
IList不继承 IList其中 IEnumerable继承IEnumerable . 如果out修饰符是大多数执行 IList 的唯一原因(例如 Collection , List )实现 ILis
尝试过: IList> matrix = new List()>(); 可是我做不到。我该怎么做?我需要一个字符串矩阵... 最佳答案 你需要: IList> matrix = new List>()
我有一个构建列表列表的方法。我想让返回类型使用通用 IList<> 接口(interface)来减少与下游具体 List<> 类型的耦合。但是,编译器在类型转换方面遇到了困难。 public ILis
我有一个 IList我想将此集合复制到 IList收藏 我试过这个: IList listAdminVAT = new AdministrationService(session).ListDecim
我有一个实现了 IList 的对象接口(interface),我想把它转换成IList或者 List ,我试过了 IList a=(IList)b; List a=(IList)b; IList a=
我似乎无法弄清楚为什么以下内容不起作用。 通过阅读,它似乎一定与 this 之类的东西有某种关联。 . public class Test { public void SomeFunction
我有几个类: class Vehicle { } class Car : Vehicle { } 我有一个派生类的列表: IList cars; 我想将列表转换为其基类,并尝试过: IList bas
List的定义在 .net 中显示它实现了各种接口(interface)。 public class List : IList, ICollection, IEnumerable, IList, IC
我想创建一个 ListCollectionView使用通用 IList但是构造函数得到一个非泛型 IList .如何将我的列表从 IList 转换为至 IList ? 如果有另一种方法可以创建 Lis
这个问题在这里已经有了答案: Remove items from one list in another (10 个答案) 关闭 8 年前。 很抱歉提出这样的基本问题,我是 LINQ 的新手,我正试
我正在实现 IListSource这需要一个方法 GetList()具有以下签名: IList GetList() 我正在使用 .NET Framework 2,我想返回一个实现 IList 的对象,
我有一个类(class)想要 IList , 但我有一个 Systems.Collection.IList , 来自 NHibernate 问题。 我想创建一个将其转换为 IList 的方法.我该怎么
我似乎找不到为我的 MVVM Xamarin 表单应用程序构建和使用数据的最佳实践或最佳机制。这可能是对如何解决问题的简单误解,但我不确定。 我曾尝试使用 IEnumerable、IList 和一个简
多年来,我们大部分时间都在使用泛型集合。有时我们确实需要任何东西的集合(好吧,通常只有一些不同的东西但没有公共(public)基类)。对于这种情况,我们可以使用 IList或通用 IList作为方法参
我有一个 IList目的。 Person 类有字段,FirstName , LastName等 我有一个函数需要 IList我想以与 IList 相同的顺序传入 FirstNames 列表目的。有没有
尝试这样转换 Products1 = (IList)basicProfile.Products2.Select(ToProductInfo) Products1 是一个 public IList Pr
我正在尝试创建一个列表列表,但在实例化列表时遇到了问题。 IList> allLists = List>(); 我遇到了这一行的编译错误。 最佳答案 你必须实例化一个 List的 IList , 不是
我有一个集合,它实现了一个扩展 IList 和 List 的接口(interface)。 public Interface IMySpecialCollection : IList, IList {
我是一名优秀的程序员,十分优秀!