Sort a List<T> using query expressions
Asked Answered
L

7

13

I have a problem using Linq to order a structure like this :

public class Person
{
    public int ID { get; set; }
    public List<PersonAttribute> Attributes { get; set; }
}

public class PersonAttribute
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Value { get; set; }
}

A person might go like this:

PersonAttribute Age = new PersonAttribute { ID = 8, Name = "Age", Value = "32" };
PersonAttribute FirstName = new PersonAttribute { ID = 9, Name = "FirstName", Value = "Rebecca" };
PersonAttribute LastName = new PersonAttribute { ID = 10, Name = "LastName", Value = "Johnson" };
PersonAttribute Gender = new PersonAttribute { ID = 11, Name = "Gender", Value = "Female" };

I would like to use LINQ projection to sort a list of persons ascending by the person attribute of my choice, for example, sort on Age, or sort on FirstName.

I am trying something like

string mySortAttribute = "Age"
PersonList.OrderBy(p => p.PersonAttribute.Find(s => s.Name == mySortAttribute).Value);

But the syntax is failing me. Any clues?

Lukey answered 30/3, 2009 at 3:18 Comment(0)
D
9

OrderBy is a LINQ extension that produces a new sequence. To order the existing sequence you need to add an extension method or two... then you can use:

PersonList.Sort(p => p.Attributes.Find(
  s => s.Name == mySortAttribute).Value);

public static class ListExtensions {
  public static void Sort<TSource, TValue>(
    this List<TSource> source,
    Func<TSource, TValue> selector)
  {
    var comparer = Comparer<TValue>.Default;
    source.Sort((x, y) => comparer.Compare(selector(x), selector(y)));
  }
  public  static void SortDescending<TSource, TValue>(
    this List<TSource> source,
    Func<TSource, TValue> selector)
  {
    var comparer = Comparer<TValue>.Default;
    source.Sort((x, y) => comparer.Compare(selector(y), selector(x)));
  }
}
Dedifferentiation answered 30/3, 2009 at 6:17 Comment(0)
H
8

I know this is an old post, but I thought I'd post a comparer I found a while ago in case anyone else needs it.

public class GenericComparer<T> : IComparer<T>
 {
     public string SortExpression { get; set; }
     public int SortDirection { get; set; } // 0:Ascending, 1:Descending

     public GenericComparer(string sortExpression, int sortDirection)
     {
         this.SortExpression = sortExpression;
         this.SortDirection = sortDirection;
     }
     public GenericComparer() { }

     #region IComparer<T> Members
     public int Compare(T x, T y)
     {
         PropertyInfo propertyInfo = typeof(T).GetProperty(SortExpression);
         IComparable obj1 = (IComparable)propertyInfo.GetValue(x, null);
         IComparable obj2 = (IComparable)propertyInfo.GetValue(y, null);

         if (SortDirection == 0)
         {
             return obj1.CompareTo(obj2);
         }
         else return obj2.CompareTo(obj1);
     }
     #endregion
 }

Usage

List<MyObject> objectList = GetObjects(); /* from your repository or whatever */
objectList.Sort(new GenericComparer<MyObject>("ObjectPropertyName", (int)SortDirection.Descending));
dropdown.DataSource = objectList;
dropdown.DataBind();

You could overload the constructor to accept the SortDirection enum. I didn't do this because the class is in a library without a reference to System.Web.

Hag answered 22/4, 2010 at 15:32 Comment(0)
E
5

Why don't you use a key-value dictionary instead of your List<PersonAttribute> ? It would suit better, i think, and make everything else easier.

Update - like this:

public class Person
{
  public Dictionary<string, string> Attributes = new Dictionary<string,string>();
}

List<Person> people = new List<Person>();

Person rebecca = new Person();
rebecca.Attributes["Age"] = "32";
rebecca.Attributes["FirstName"] = "Rebecca";
rebecca.Attributes["LastName"] = "Johnson";
rebecca.Attributes["Gender"] = "Female";
people.Add(rebecca);

var PeopleInAgeOrder = people.OrderBy(p => p.Attributes["Age"]);
Enfeoff answered 30/3, 2009 at 3:28 Comment(1)
This doesn't answer his question. He didn't ask how to restructure his data, he asked how to sort a (presumably existing, and probably unchangeable legacy) data structure.Bawl
S
1

This is assuming that Attribute class implement IComparable or has a nice ToString function (i hope).

var list = personList.OrderBy(p => p.Attributes.FirstOrDefault(a => a.Name == "Age"))

Otherwise the syntax gets more convoluted:

var list = personList
            .OrderBy(p => 
                     p.Attributes.FirstOrDefault(a => a.Name == "Age") == null ?
                     "" : p.Attributes.First(a => a.Name == "Age").Value
            );

I also assume that you have one value for each key - otherwise you'd need to have smarter code... ;-)

Saransarangi answered 30/3, 2009 at 5:39 Comment(0)
N
0

Could it be that your syntax is wrong? Your property is called Attributes but your using something called ObjectSettings in code? Or is that a typo.

If it is then your code looks fine unless not all Person instances have the Attribute you're trying to order by in which case you'd get an exception.

EDIT: Also, instead of using Find, try using First.

PersonList.OrderBy(p => p.Attributes.First(a => a.Name == "Age").Value)
Newish answered 30/3, 2009 at 3:42 Comment(1)
I have fixed the typo, found out my problem was with IQueryable and List, which required a Cast. Thanks for helping out.Lukey
E
0

I'd imagine that you're getting an exception where one item doesn't have an age attribute. I tried the below code, and it worked fine - I'm guessing your data is a bit off, as pointed out by other posters. Anyway, the below works fine...

    List<Person> personList = new List<Person>();
    Random rand = new Random();

    //generate 50 random persons
    for (int i = 0; i < 50; i++)
    {
        Person p = new Person();
        p.Attributes = new List<PersonAttribute>();
        p.Attributes.Add(new PersonAttribute() { ID = 8, Name = "Age", Value = rand.Next(0, 100).ToString() });
        p.Attributes.Add(new PersonAttribute() { ID = 10, Name = "Name", Value = rand.Next(0, 100).ToString() });
        personList.Add(p);
    }

    var finalList = personList.OrderBy(c => c.Attributes.Find(a => a.Name == "Age").Value).ToList();
Elkins answered 30/3, 2009 at 4:1 Comment(0)
M
0

Some cases you need to consider:

  • Since your attribute is in string an age of "30" and "3" will be ordered before an age of "4"
  • The attribute might not exist

If you create this extension methods class:

public static class ListExtenstions
{
    public static List<Person> OrderList(this List<Person> list, string attributeName, PersonAttribute defaultAttribute)
    {
        return OrderList(list, attributeName, defaultAttribute, x => x);
    }

    public static List<Person> OrderList<T>(this List<Person> list, string attributeName, PersonAttribute defaultAttribute, Func<string, T> convertion)
    {
        return list.OrderBy(x => convertion((x.Attributes.FirstOrDefault(y => y.Name == attributeName) ?? defaultAttribute).Value)).ToList();

        // Query Syntax
        //return
        //    (from p in list
        //     let attribute = p.Attributes.FirstOrDefault(a => a.Name == attributeName) ?? defaultAttribute
        //     orderby attribute.Value
        //     select p).ToList();
    }
}

You can then sort the list correctly in this manner:

List<Person> persons = ...
...
PersonAttribute defaultAttribute = new PersonAttribute() { Value = "0" };
var ordered = persons.OrderList("Age", defaultAttribute, x => Convert.ToInt32(x));

This will give correct sorting order. If the attribute will always be present you could remove defaultAttribute.

To sort on 'Name' just use:

List<Person> persons = ...
...
PersonAttribute defaultAttribute = new PersonAttribute() { Value = String.Empty };
var ordered persons.OrderList("Name", defaultAttribute);
Mullens answered 23/4, 2010 at 8:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.