如何通过自定义顺序订购IEnumerable [英] How to order an IEnumerable by custom order

查看:79
本文介绍了如何通过自定义顺序订购IEnumerable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

偷看,



我有一个IEnuermerable< customclass>我想知道可以做文件.OrderBy(d => d.Type。 Id)但我不想要它订购1-5等,我想要一个自定义的类型订单。如下



3,5,2, 4,6(这是为这些文件创建的按钮,但我的老板希望现在按顺序显示它们)



任何帮助这样做都会非常感激

解决方案

您可以使用字典将Type Ids映射到您自己的订单标准,例如

字典< int,int> mo =  new 字典< int,int> {{ 3  0 },{ 5  1 },{ 2  2 },{ 4  3 },{ 6  4 }}; 



然后

 documents.OrderBy(d = >  mo [d.Type.Id])


您可以尝试实现自定义 IComparer< int> ;:

  public  VerySpecialInt32Comparer:IComparer< int> 
{
public int 比较( int left, int right){
int result;
if (left == right){
result = 0 ;
}
如果(左== 3 ){
结果= 1 ;
}
else if ((left == 5 )&&(右!= 3 )){
result = 1 ;
}
else if ((left == 2 )&&((右!= 3 )&&(右!= 5 )){
result = 1 ;
}
else if ((left == 4 )&&((right != 3 )&&(右!= 5 )&&(右!= 2 )){
result = 1 ;
}
< span class =code-keyword> else
{
result = -1;
}
return result;
}
}



一旦定义 IComparer< T> 实施,你将会必须在 Li上使用它ST< INT> 。通过这种方式将 IEnumerable< int> 转换为 List< int>

< pre lang =c#> 使用 System.Linq;

IList< int> list = iEnumVariable.ToList();



最后使用特定的比较器实现:

 list.Sort(  new  VerySpecialInt32Comparer()); 





警告!我考虑到只有5个值可能(2,3,4,5和6)。如果它们中有更多,那么这种算法将不起作用;你必须根据自己的需要调整它。



希望这会有所帮助。祝你好运。


Hi peeps,

i've got an IEnuermerable<customclass> documents, i'm wanting to sort this IEnumerable by a custom order, of TypeId

now i know could do documents.OrderBy(d => d.Type.Id) but i'm dont want it to order 1-5 etc, i would like a custom order of type.Id as following

3,5,2,4,6 (this is a button is being created for these documents, but my boss wishes to have them displayed in this order now)

any help on doing this would be very grateful

解决方案

You may use a dictionary to map the Type Ids to your own order criterion, e.g.

Dictionary<int, int> mo = new Dictionary<int,int>{ { 3, 0 }, { 5, 1 }, {2,2}, {4,3}, {6,4} };


Then

documents.OrderBy(d => mo[d.Type.Id])


You could try to implement a custom IComparer<int>:.

public VerySpecialInt32Comparer : IComparer<int>
{
   public int Compare(int left, int right) {
      int result;
      if (left == right) {
         result = 0;
      }
      if (left == 3) {
         result = 1;
      }
      else if ((left == 5) && (right != 3)) {
         result = 1;
      }
      else if ((left == 2) && ((right != 3) && (right != 5)) {
         result = 1;
      }
      else if ((left == 4) && ((right != 3) && (right != 5) && (right != 2)) {
         result = 1;
      }
      else {
         result = -1;
      }
      return result;
   }
}


Once you defined the IComparer<T> implementation, you will have to use it on a List<int>. Transform your IEnumerable<int> into a List<int> this way:

using System.Linq;

IList<int> list = iEnumVariable.ToList();


Finally use the specific comparer implementation:

list.Sort(new VerySpecialInt32Comparer());



Warning! I took into account that there are only 5 values possible (2, 3, 4, 5 and 6). If there are more of them, this alogorithm will not work; you will have to adapt it to your needs.

Hope this helps. Good luck.


这篇关于如何通过自定义顺序订购IEnumerable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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