U4-888 - Sort order of items is only updated if you "sort" the items?

Created by Pete Duncanson 20 Sep 2012, 15:21:57 Updated by Pete Duncanson 27 Feb 2014, 11:46:20

Relates to: U4-873

Noticed this today.

Given a folder with some content in it like so:

folder

  • prod1 (not published)
  • prod2 (published)
  • prod3 (published)

We render these out on the site in a list and they show up as:

  • prod2
  • prod3

Publish prod1 and the list looks like this:

  • prod2
  • prod3
  • prod1

Even though in the back end prod1 should come first. Tried republishing the parent folder (including children) but the order remained the same.

To get the order right I had to right click "sort", not sort anything and just click save which must have updated the sort order anyway. Then it works...

Something not getting updated when it should there?

1 Attachments

Comments

Sebastiaan Janssen 01 Oct 2012, 09:48:37

Hey Pete, could this be a duplicate of issue #U4-708 ?? I think this should be fixed now.


Pete Duncanson 01 Oct 2012, 12:55:22

Not sure. Can't say I've checked the order order that is being saved in the umbraco.config file to check. Its possible I suppose. Got a nightly you could try it out on?


Sebastiaan Janssen 01 Oct 2012, 14:12:44

While the order in umbraco.config still is the order in which you published them, in a recent nightly, the sort order at least is correct (following the steps above). See the attached screenshot, sortorder is at the end of the line. I think this should be sufficient to solve your problem?


Pete Duncanson 01 Oct 2012, 14:28:30

Hmmm not sure that is going to do it. When I publish the order of the nodes in umbraco.config should reflect the order of the nodes in the admin tree. Currently it looks like it will always append the new published node to the end of the parent which is not right. The #U4-708 fixes an issue with the incorrect sort order being set for a published node but this is an issue with that sort order not being applied when an existing node is published.

I think it needs to run a re-sort when you do a publish (assuming its not the last element if you wanted to optimise it a little). Currently I think it just appends and skips the sort.


Funka! 01 Oct 2012, 16:50:33

Seems related to issue I reported the other week, "U4-873 Media Items not returning in correct sort order any more??" However with media, there is no publish/unpublished state, even though symptoms are exact same here.


Tim Payne 02 Oct 2012, 08:37:17

This has been there for a while. Basically, when you publish a new item, it's added to the end of the nodes it sits with, rather than in the right place (the sort order IS correct though). You can get round it by ordering by sortOrder if I recall correctly. I've been meaning to look into this in more detail, but haven't had the time recently!


Funka! 19 Oct 2012, 01:31:58

Hi Sebastiaan, you mentioned back on October 01 that you thought this was a duplicate of U4-708, which I see has been marked resolved in v4.9.0, but I think these are different issues since the symptom still persists in 4.9.0 final.

U4-708 seems to me to have been about the actual {{sortOrder}} attribute not being generated correctly due to existing gaps in the sequence. This issue here U4-888 seems to be the new breaking behavior since 4.8.0 of nodes no longer being implicitly sorted by their sortOrder (i.e., the same as you see them in the admin tree), but instead are returned somewhat inconsistently according to either which node was last published or whenever you last explicitly sorted them. I guess as Tim says, we just need to remember to OrderBy("sortOrder") from now on and no longer rely on implicit sorting working for us as expected? (It does seem though like "sortOrder" in this case is only doing half its job! i.e., working in the backend, not so much in the frontend.) Thx!


Sebastiaan Janssen 19 Oct 2012, 03:44:46

@Funka Yeah I might've done, but I realized this is a different issue which has been there for as long as I can remember, I know I ran into this behavior at least 2 years ago. So yeah, recommend doing explicit sorting for now until we can sort this out (see what I did there? ;-) ).


Stephan 19 Oct 2012, 10:36:29

The way we picked "the first node of the tree" in pre-4.10 was... not so clean. Has been entirely refactored in 4.10 and in theory we now pick "the node with the lowest @sortOrder" so whatever the actual order in the XML cache, it's the actual @sortOrder attribute that will be used.


Funka! 20 Oct 2012, 01:38:22

Hello Umbraco Team and Umbraco Enthusiasts! Per Tim's suggestion to add {{OrderBy("sortOrder")}} to our Razor scripts so we can get our nodes in the expected order, one of our developers tonight replied thusly to me: "Good idea, but I could not get it to work. It crashes the script. I can sort by 'Id', or 'Name', or 'UmbracoFile', for example, but not 'sortOrder' or 'SortOrder'."

After questioning this further, here's what else he had to say:

"Trying to access {{@photo.sortOrder}} reveals that it is of type {{umbraco.MacroEngines.DynamicNull}}, and {{@photo.SortOrder}} simply crashes the page." He further concluded that: "In both cases the error is at {{umbraco.MacroEngines.ExamineBackedMedia.get_SortOrder()}}. I looked at the source, and in fact the {{ExamineBackedMedia}} class throws a "not implemented" exception in the get method for property SortOrder. I don't know enough about the Umbraco architecture to conclude anything other than, OK, I guess it is 'not implemented'."

Any suggestions on how we can get our (media) nodes to appear in the correct/expected/natural order? Am I off-base here in thinking this isn't a "normal" bug but rather perhaps a "major" one with no workaround??

Thanks!


Sebastiaan Janssen 22 Oct 2012, 08:00:46

@Funka! Yup, looks like sortorder is missing. The workaround for media items (and this issue is actually about content, so seperate issue) is to use the Media API, like so, it will reflect whatever the sorting in the media section is:

@using umbraco.cms.businesslogic.media

var startMedia  = new Media(1063);

foreach(Media childMedia in startMedia.GetChildMedia())
{
    @childMedia.Text - @childMedia.getProperty("umbracoFile").Value<br />
}


Funka! 07 Nov 2012, 20:16:32

Hi Sebastiaan,

You mention that this particular issue (U4-888) is about Content, not Media, although certainly the title of this issue and the symptoms presented seem like they are the exact same problem at first glance. (Although of course we don't have an XML cache for media items so there surely is something different going on...)

I did create another issue U4-873 specifically regarding Media and their sort order about three days prior to this particular one being created, but there hasn't been any activity on that issue.

Your suggestion to use {{GetChildMedia()}} on the {{umbraco.cms.businesslogic.media.Media}} class does work I suppose, although I noticed this is actually a uQuery extension method and not on the class itself, which I thought I read somewhere was going to be removed soon, if not already? So hopefully an Umbraco version upgrade doesn't take away this last saving hope for getting my items returned in the correct/expected order! Thanks for looking into this.


Sebastiaan Janssen 08 Nov 2012, 09:00:39

@Funka I don't think it's the same issue, the API's for both diverge a bit I think so it's weird. GetChildMedia is not going anywhere, but in fact under the covers it's doing nothing but:

foreach (var child in media.Children) { yield return child; }

So you could probably just do:

foreach(Media childMedia in startMedia.Children) { @childMedia.Text - @childMedia.getProperty("umbracoFile").Value
}

It's annoying that the Media and DynamicMedia implementations are also slightly different, so you get different results on them as well. We've just moved the Razor implemtation into a better structure and it should now be easier to start fixing these differences. Not for 4.10.0 yet unfortunately though.


Funka! 09 Nov 2012, 00:36:25

Yeah, I noticed that as well with GetChildMedia() after I posted. So "Children" from {{umbraco.cms.businesslogic.media.Media}} do get returned in correct/native sort order, but "Children" from {{umbraco.MacroEngines.DynamicMedia}} do not. Noted!

Although, since I sometimes prefer working with DynamicMedia in my razor scripts for its simplicity and other functionality, I wanted to be able to both have my cake AND eat it too---so a little bit of doubling up came to pass...

foreach (Media childMedia in startMedia.Children) { var dynMedia = Library.MediaById(childMedia.Id); // DynamicMedia

  • @dynMedia.Name
  • }

    Regardless, it would certainly be much simpler for both Content and Media nodes if we could have them consistently return in their native/expected order. Looking forward to the improvements whenever they're ready. Thanks again!


    Sebastiaan Janssen 09 Nov 2012, 08:07:39

    Hehe nice, though you might incur a little bit of overhead because you're fetching the media item twice.


    Sebastiaan Janssen 04 Apr 2013, 14:18:27

    Removing due in 6.1 as this was already implemented in an earlier version, which means it's included in all later versions as well.


    Pete Duncanson 27 Feb 2014, 11:46:20

    Just upgraded a 4.10 site to 6.1.6 and still seeing this behaviour. But I think it was due to us using GetDocumentsOfDocumentType. We used it to get all FAQ objects which can only live under a FAQ Index page. As a result we expected it to return them sorted but it does not which now I've thought about it is correct behaviour as it could get content from multiple folders so how would it know how to sort it.

    var categories = Document.GetDocumentsOfDocumentType( 1234 );

    Quick fix was to sort the list when it came back as we knew the context we where pulling them back from.

    This comment is more for reference for anyone coming behind seeing the same issue, its by design and correctly so.


    Priority: Normal

    Type: Bug

    State: Fixed

    Assignee: Shannon Deminick

    Difficulty: Normal

    Category:

    Backwards Compatible: True

    Fix Submitted:

    Affected versions: 4.8.0, 4.9.0, 4.10.0, 4.11.0, 6.0.0, 4.9.1, 4.11.1, 4.11.2, 4.11.3, 4.11.4

    Due in version: 6.0.1, 4.11.5

    Sprint:

    Story Points:

    Cycle: