-2

I have a model:

public class SomeClass {
  public List<Person> Owner {get; set;}
  public int Balance {get;set;}
  . . . .
}

and

public class Person {
  public string Name {get;set;}
  public Guid PersonID {get;set;}
  . . . .
}

I have a wrapper class which handles the sorting:

public class SortedResult <ModelType>: Result where ModelType: new() {

  . . . .

  private void SetProperties(IQueryable <ModelType> data, int pageIndex, int pageSize, string orderBy, OrderDirection ? orderDir) {
    if (!String.IsNullOrEmpty(orderBy)) {
      // auto-handles the order-by
      if (orderDir == OrderDirection.Descending) {
        data = data.OrderByDescending(orderBy);
      } else {
        data = data.OrderBy(orderBy);
      }
    }

    int total = data.Count();
    // no ordering
    Data = data.Skip(pageIndex * pageSize).Take(pageSize).ToList();
    AllData = myIncludeAllData ? data : null;
    PageIndex = pageIndex;
    PageSize = pageSize;
    TotalSize = total;
  }

}

As you can see, SortedResult allows me to sort any List<T> dynamically. Sorting using other properties of SomeClass runs ok but when I sort by the Owner property, I'm getting error:

"At least one object must implement IComparable."

I am guessing that it's because the system doesn't know how to sort List<SomeClass> by the Owner property.

How can I allow SomeClass to be sorted by Owner.Name if it is sorted using Owner? I have seen examples that overrides the Compare logic of the model but I don't know where to start.

Any help will be very much appreciated.

EDIT:

@dasblinkenlight's comment enlightened me. Indeed, it is wrong to sort by Owner which is a List<Person>.

For the fix, I modified my SomeClass model:

public class SomeClass {
  public List<Person> Owner {get; set;}
  //should sort using this prop instead of Owner
  public string OwnerName { get 
                             { return string.Join(", ", this.Owner.Select(r => r.Name)); }
                          }
  public int Balance {get;set;}
  . . . .
}

Thanks for all the effort everyone.

xGeo
  • 2,149
  • 2
  • 18
  • 39
  • `Owner` is a `List` object, not a `Person` object with a `Name` property. – Sergey Kalinichenko Oct 18 '17 at 13:51
  • correct @dasblinkenlight. Is there something wrong with that? – xGeo Oct 18 '17 at 14:03
  • There are many `Person` objects in a list, each having its own `Name` If you are sorting `SomeClass` on `Owner`, which of several `Person` objects are you going to pick for `Name` comparison? – Sergey Kalinichenko Oct 18 '17 at 14:06
  • The reason is that `Person` does not implement `IComparable` (but the other properties do) so there is no way to `OrderBy` on the `Person` class.. It should work if you add the syntax to implement the interface to your class: `public class Person : IComparable { public int CompareTo(object obj) { var other = obj as Person; return other == null ? 1 : String.Compare(Name, other.Name, StringComparison.Ordinal); } }`. – Rufus L Oct 18 '17 at 14:11
  • @GeomanYabes Actually implementing IComparable is more sound advise then IComparable. Just for you to know. – Kostya Oct 18 '17 at 16:48

2 Answers2

1

Make some changes to Person class:

public class Person : IComparable {
  public string Name {get;set;}
  public Guid PersonID {get;set;}
  // implement the methods for IComparable
}

you can find a good sample in this link.

https://msdn.microsoft.com/en-us/library/system.icomparable(v=vs.110).aspx

Ehsan Zargar Ershadi
  • 24,115
  • 17
  • 65
  • 95
0

Person should implement IComparable<T> interface. Check the official docs for an example.

Kostya
  • 849
  • 5
  • 14