سؤال

Possible Duplicate:
Sorting an IList in C#

I have an IList<Keyword> where Keyword is a class containing a title(string) and a key(number). I wish to sort this IList based on the key. Is there an inbuilt C# functionality that I can use?

هل كانت مفيدة؟

المحلول

You need to add: using System.Linq;

IList<keyword> sortedList = list.OrderBy(r => r.key).ToList();

Or you can try:

  IList<keyword> sortedList2 = (from r in list
                                orderby r.key
                                select r).ToList();

نصائح أخرى

UPDATE: You can use OrderBy orOrderByDescending

var sort = list.OrderBy(x=> x.key);

Or

var reverseSort = list.OrderByDescending(x=> x.key);

Here is a link to OrderByDescending which you can use if you have IList<YourType> or IEnumerable<YourType>.

 var sorted = list.OrderByDescending(x=> x.key);

Try this:

var sorted = from c in list orderby c.Key select c;

You can use Linq Enumerable.OrderBy Method for this:

var result = listOfObject.OrderBy(item => item.key).ToList();

for sorting in descending order use Enumerable.OrderByDescending Method..

If you are using .net framework 3.0 and above then include using System.Linq on top and try above extension method.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using dynamic = System.Linq.Dynamic;
using System.Linq.Expressions;

namespace Lib.Extensions
{
    public static class Utils
    {
        /// <summary>
        /// Sorts by Model property with dynamic expression
        /// </summary>
        /// <param name="list"> </param>
        /// <param name="propertyName"></param>
        /// <param name="sortDirection"> </param>
public static List<TObject> SortByModelProperty<TObject, TProperty>(this List<TObject> list, Expression<Func<TObject, TProperty>> property, SortDirection sortDirection)
    {
        string propertyName = GetPropertyName(property);
        string exp1 = string.Format("model.{0}", propertyName);

        var p1 = Expression.Parameter(typeof(TObject), "model");
        var e1 = System.Linq.Dynamic.DynamicExpression.ParseLambda(new[] { p1 }, null, exp1);

        if (e1 != null)
        {
            if (sortDirection == SortDirection.Ascending)
            {
                var result = list.OrderBy((Func<TObject, TProperty>)e1.Compile()).ToList();
                return result;
            }
            else
            {
                var result = list.OrderByDescending((Func<TObject, TProperty>)e1.Compile()).ToList();
                return result;
            }
        }
        return list;
    }

    private static string GetPropertyName<TObject, TProperty>(Expression<Func<TObject, TProperty>> property)
    {
        MemberExpression memberExpression = (MemberExpression)property.Body;
        PropertyInfo propertyInfo = (PropertyInfo)memberExpression.Member;

        return propertyInfo.Name;
    }

    }
}

You should add System.Linq.Dynamic.You can find it Nuget package manager. I think that This extension method helps you.

Yes with help of extension method you can easily achieve this

var sortelist = list.OrderBy(x=>x.PropertyName).ToList();

MSDN :

To sort data Ascending :

Enumerable.OrderBy Method (IEnumerable, Func) - Sorts the elements of a sequence in ascending order according to a key.

To sort data Descending :

Enumerable.OrderByDescending Method (IEnumerable, Func) - Sorts the elements of a sequence in descending order according to a key.

If you have control over the Keyword class and consider the number ordering its "natural" ordering, have Keyword implement the IComparable<Keyword> interface.

If you don't have control over the class or you don't want to specify this ordering as natural/default, create a class which implements the IComparator<Keyword> interface.

EDIT: Or use LINQ for a simpler, quicker answer. Been stuck in Java too long!

var sorted = keywords.OrderBy(k => k.Key);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top