-1

I have a List<Role> (see below) that I am binding to an asp.net gridview. I want to sort this data using SortExpression, such that it is sorted by two properties of sub-objects of the rows. Specifically, I want to sort by the Application's Name, then the ApplicationType's ApplicationTypeName.

How can I do this?

The classes here are:

public class Application
{
    public string Name {get; set;}
    public int Status {get; set;}
}

public class ApplicationType
{
    public string ApplicationTypeName {get; set;}
    public int ApplicationTypeStatus {get; set;}
}

public class Role
{
    public Application oApplication {get; set;}
    public ApplicationType oApplicationType {get; set;}
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
nabukhas
  • 193
  • 1
  • 9
  • I realise this is your first stackoverflow question, but you *might* want to think a bit more about: "what information would the reader need to answer my question?" - for example, it seems the *actual* question is about asp.net gridview, which isn't even hinted at in the question; additionally the question title says "array", the example says `List`, and the tags say "arraylist" - these 3 things are **completely different and mutually exclusive**. The tags also say "linq", which isn't evident anywhere else... We're happy to help, but we aren't psychic. I will edit to illustrate... – Marc Gravell Oct 13 '14 at 10:55

1 Answers1

1

Edit: note that I was responding to the earlier verison of the question, before it related to gridview; still, this might be useful...

Worst case: you can use the approach here to pre-sort the list before binding it to the gridview.


Various options:

  • implement IComparable[<T>]
  • implement IComparer[<T>]
  • use an ad-hoc sort

I'm guessing you just need the last, so perhaps:

list.Sort((x,y) => {
    int delta = string.Compare(x.Application.Name, y.Application.Name);
    if (delta == 0) delta = string.Compare(
        x.ApplicationType.ApplicationTypeName, y.ApplicationType.ApplicationTypeName);
    return delta;
});

Alternatively, you can perhaps do it via LINQ in the source data - note however that this is done when creating a new list - it isn't an in-place sort of an existing list:

var list = source.OrderBy(x => x.Application.Name)
                 .ThenBy(x => x.ApplicationType.ApplicationTypeName)
                 .ToList();
Community
  • 1
  • 1
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Marc Gravell :: thanks for answer : this list to be bonded to Gridview so when i want to sort by Application the sortExpresion will be "Application.Name" how this will be done ? – nabukhas Oct 13 '14 at 10:38
  • @user2516342 back up a moment; you haven't even told me what UI implementation you are using; the question doesn't say anything about `Gridview`, etc; where are you using/seeing `sortExpression`? – Marc Gravell Oct 13 '14 at 10:44
  • :: My mistake i am using asp.net GridView to bind List in it And some column view property in class Application.Name or ApplicationType.Name. – nabukhas Oct 13 '14 at 10:50
  • @user2516342 I don't know whether Gridview supports sorting by properties of sub-objects – Marc Gravell Oct 13 '14 at 10:53
  • :: the sorting is custom so nothing to do with GridView – nabukhas Oct 14 '14 at 06:02
  • @nabukhas if you are trying to sort via `SortExpression`, it has *everything* to do with gridview... – Marc Gravell Oct 14 '14 at 06:52
  • Thank you Marc Gravell the following [Question](http://stackoverflow.com/questions/366332/best-way-to-get-sub-properties-using-getproperty) solve my problem. – nabukhas Oct 14 '14 at 10:08