Note that there are some explanatory texts on larger screens.

plurals
  1. POSorting IList<Person> by parent object which can be null
    text
    copied!<p>I've been looking through the search system and not come across an answer for my problem, while these questions are similar and I even used some of them to make a start I've hit a brick wall.</p> <p>I've already checked out these questions</p> <ul> <li><a href="https://stackoverflow.com/questions/867164/algorithm-for-sorting-a-list-of-objects-in-c-sharp">Algorithm for sorting a list of objects in c#</a></li> <li><a href="https://stackoverflow.com/questions/15486/sorting-an-ilist-in-c-sharp">Sorting an IList in C#</a></li> </ul> <p>I have an object brought back by nHibernate called Person as follows</p> <pre><code> public class Person : IPerson, IComparable&lt;Person&gt;{ // private properties private int _id; private string _name; private Person _parent; private IList&lt;Person&gt; _children; private IList&lt;Group&gt; _groups; // public properties public virtual int id { get { return _id; } set { _id = value; } } public virtual string name { get { return _name; } set { _name= value; } } public virtual Person Parent { get { return _parent; } set { _parent = value; } } public virtual IList&lt;Person&gt; Children { get { return _children; } set { _children = value; } } public virtual IList&lt;Group&gt; Groups { get { return _groups; } set { _groups = value; } } // constructor public Person() {} // this section I added after looking on SO public virtual Int32 parentid { get { if (_parent == null) { return _id; } else { return _parent.id; } } } public virtual Int32 CompareTo(Person obj) { if (this.id == obj.id) return 0; return this.parentid.CompareTo(obj.parentid); } } </code></pre> <p>Then a method in my Dao which brings back a list of all the Person Objects in the database under a group. Unfortunately it brings them back in the order they were entered and I want to sort them into a parent child relationship.</p> <pre><code> i.e. (id,name,parent) person 1 (1,"Alice",null) person 2 (2,"Bob",1) person 3 (3,"Charlie",null) person 4 (4,"Dejgo",2) // child of a child person 5 (5,"Edgar", null) person 6 (6,"Florence", 3) </code></pre> <p>When I do a sort as mentioned in this <a href="https://stackoverflow.com/a/867250/728841">answer</a> like so:</p> <pre><code> class PeopleBLL : IPeopleBLL { // spring properties private IGroupsBLL _groupsBLL; private IPeopleDao _peopleDao; // public properties where spring sets up the links to the other dao and bll methods public IGroupsBLL { set{_groupsBLL = value;} } public IPeopleDao { set{_peopleDao= value;} } // constructor public PeopleBLL() { } // method we are interested in public IList&lt;Person&gt; GetAllInGroup(int groupID){ //get the group object Group groupObject = _groupsBLL.Get(groupID); // get the people in the group IList&lt;Person&gt; people = _peopleDao.GetAllInGroup(groupObject); // do something here to sort the people people.Sort(); // return the list of people to aspx page return people; } } </code></pre> <p>I get the list in the format</p> <pre><code> person 2 person 1 person 6 person 3 person 5 person 4 </code></pre> <p>but I want it in the format</p> <pre><code> person 1 person 2 person 4 person 3 person 6 person 5 </code></pre> <p>Any ideas?</p> <p><strong>EDIT:</strong> </p> <p>I didn't put the rest of this in because I didn't want to confuse the question by all the extra technologies used but since I was asked. Spring.Net is used to link up all the objects, I have a 3-tier architecture Front end (asp.net pages), Business layer, and Dao layer which communicates with the database. I have updated the GetAllInGroup() to show everything it does but it is only the sorting I am interested in. The Dao uses an nHibernate Criteria Query to get all the objects under the group as follows.</p> <pre><code> public IList&lt;Person&gt; getRegistrationsForDonor(Group groupObject) { IList&lt;Person&gt; rv = CurrentSession.CreateCriteria(typeof(Person),"p") .CreateCriteria("Groups","g") .Add(Expression.Eq("g.id", groupObject.id)) .List&lt;Person&gt;(); return rv; } </code></pre> <p>And all this is started in an aspx page in an object datasource</p> <pre><code> &lt;asp:ObjectDataSource ID="ObjectDataSource1" OnObjectCreating="DataSource_ObjectCreating" runat="server" DataObjectTypeName="Domain.Person" TypeName="BusinessLayer.PersonBLL" DeleteMethod="delete" SelectMethod="GetAllInGroup" UpdateMethod="update"&gt; &lt;SelectParameters&gt; &lt;asp:ControlParameter ControlID="groupid" Type="Int32" DefaultValue="0" Name="groupID" /&gt; &lt;/SelectParameters&gt; &lt;/asp:ObjectDataSource&gt; </code></pre> <p>which is then used by a gridview, so unfortunately I need to return a sorted IList from the BLL to the front end in the BLL method as a single IList.</p> <p><strong>Edit 2</strong> </p> <p>Sorry An assumption has been made that all parents will be null at the top level and I can understand where that assumption can come from but you could in theory have only children in the group so none of the object would have a parent of null. for example the following is a valid group.</p> <pre><code> person 2 (2,"Bob",1) person 4 (4,"Dejgo",2) // child of a child person 6 (6,"Florence", 3) </code></pre> <p>which I would hope would return </p> <pre><code> person 2 person 4 person 6 </code></pre> <p><strong>Edit 3</strong></p> <p>Unfortunately all of this could not be surmised in a comment so I will have to respond here.</p> <p>Having tried @jon-senchyna answer at 17:30, I am definitely getting closer, I have just tried this on some live data and it is nearly there but I seem to be running into problems with the children coming before the parents in the group. Take the following example in Group 1 there are 48 people, I will highlight the ones of interest.</p> <pre><code> (789, "person 1", null) (822, "Person 34", null) (825, "Person 37", 789) (3060, "Person 47", 822) (3061, "Person 48", 825) </code></pre> <p>This is the order they are returned from the database, but when they are put through the sort they are in the order</p> <pre><code> Person 48 - id: 3061 Person 37 - id: 825 Person 1 - id: 789 Person 47 - id: 3060 Person 34 - id: 822 </code></pre> <p>So the children are next to the parents but in the reverse order, where I would like them in the order </p> <pre><code> Person 1 - id: 789 Person 37 - id: 825 Person 48 - id: 3061 Person 34 - id: 822 Person 47 - id: 3060 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload