列表上的自定义OrderBy

时间:2021-12-06 21:20:19

I'm trying to figure out the best way to custom sort a List. Lets say that T is a Object with a date(DateTime?) property and a status(string) property.

我想找出自定义排序列表的最佳方法。假设T是具有日期(DateTime?)属性和状态(string)属性的对象。

I have 3 cases...

我有3例…

"Urgent": I want these at the top of the list, no particular order
date = null
status = "Urgent"

“加急”:我想把它们放在列表的最前面,没有特定的订单日期= null状态=“加急”

"Normal": I want these ordered by date after the Urgent cases
date = any valid date/time
status = "On Time"

“正常”:我希望这些订单在紧急情况发生日期之后的日期=任何有效日期/时间状态=“准时”

"Later": I want these at the bottom of the list, no particular order
date = null
status = "Later"

"Later":我希望这些在列表的底部,没有特定的order date = null状态= "Later"

Any thoughts? Should I use an IQuerable object instead of List? I can always .ToList() the object later to send to my view.

任何想法吗?我应该使用IQuerable对象而不是List吗?我可以总是. tolist()对象,稍后发送到我的视图。

5 个解决方案

#1


13  

Shouldn't be too difficult, just make T implement IComparable using your comparison rules and you should be set.

不应该太难,只要用你的比较规则来实现它,你就应该被设置。

#2


32  

query = query.OrderBy(x =>
  x.Status == "Urgent" ? 1:
  x.Status == "Normal" ? 2:
  3)
  .ThenBy(x => 
  x.Status == "Urgent" ? null:
  x.Status == "Normal" ? x.Date:
  null);

Random musing: Does Ordering belong to the query, or to the class?

随机沉思:排序是属于查询还是属于类?

#3


2  

You will need to provide an implementation of IComparer, and then you can pass it in using the following overload:

您需要提供一个IComparer的实现,然后您可以在使用以下重载时传递它:

public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
    this IEnumerable<TSource> source,
    Func<TSource, TKey> keySelector,
    IComparer<TKey> comparer
)

See: http://msdn.microsoft.com/en-us/library/bb549422.aspx

参见:http://msdn.microsoft.com/en-us/library/bb549422.aspx

#4


1  

The easiest way in my opinion is to use linq :

我认为最简单的方法是使用linq:

itemsList = itemsList.OrderByDescending(ob => ob.status ).ThenBy(ob => ob.date).ToList();

#5


0  

You could just use an extension method:

你可以使用扩展方法:

Something like this...

是这样的……

public static IOrderedEmumerable<MyType> OrderForDisplay (this IEnumerable<MyType> input)
{
  return
    input
    .OrderBy(item => item.Status)
    .ThenByDescending(item => item.Status == 1 ? DateTime.MaxDate : item.date);
}

#1


13  

Shouldn't be too difficult, just make T implement IComparable using your comparison rules and you should be set.

不应该太难,只要用你的比较规则来实现它,你就应该被设置。

#2


32  

query = query.OrderBy(x =>
  x.Status == "Urgent" ? 1:
  x.Status == "Normal" ? 2:
  3)
  .ThenBy(x => 
  x.Status == "Urgent" ? null:
  x.Status == "Normal" ? x.Date:
  null);

Random musing: Does Ordering belong to the query, or to the class?

随机沉思:排序是属于查询还是属于类?

#3


2  

You will need to provide an implementation of IComparer, and then you can pass it in using the following overload:

您需要提供一个IComparer的实现,然后您可以在使用以下重载时传递它:

public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
    this IEnumerable<TSource> source,
    Func<TSource, TKey> keySelector,
    IComparer<TKey> comparer
)

See: http://msdn.microsoft.com/en-us/library/bb549422.aspx

参见:http://msdn.microsoft.com/en-us/library/bb549422.aspx

#4


1  

The easiest way in my opinion is to use linq :

我认为最简单的方法是使用linq:

itemsList = itemsList.OrderByDescending(ob => ob.status ).ThenBy(ob => ob.date).ToList();

#5


0  

You could just use an extension method:

你可以使用扩展方法:

Something like this...

是这样的……

public static IOrderedEmumerable<MyType> OrderForDisplay (this IEnumerable<MyType> input)
{
  return
    input
    .OrderBy(item => item.Status)
    .ThenByDescending(item => item.Status == 1 ? DateTime.MaxDate : item.date);
}