Trouble sorting pages for user friendly sitemap

Vote:
 

Hello,

In addition to our XML sitemap, I'm trying to create a sitemap that list all our pages inside an unordered list. The recursive function I've written (see below) works getting all the pages, but the order isn't correct despite using the OrderBy() method.

I need to output all the pages in the same order they appear in Edit mode, so my initial thought was to arrange my results based on their sort index. I went through my content tree and made sure each parent node had it's Sort subpages field set to According to sort index and adjusted all the values. Everything looks and acts correctly within the Edit view, but when I print out my results in the View file I'm not sure how it's determining it's order.

Is there something I missing?

Here's the function I wrote to get all the pages:

protected void DescendContentTree (IContent page, Hashtable tree)
{
  var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
  var check = contentRepository.GetChildren<PageData>(page.ContentLink)
    ?.Where(x => !(x.StopPublish < DateTime.Now))
    ?.OrderBy(x => x.SortIndex);
  Hashtable children;

  if (check.Any())
  {
    children = new Hashtable();

    foreach (PageData item in check)
    {
      DescendContentTree(item, children);
    }
  }
  else
  {
    children = null;
  }

  tree.Add(page, children);
}

And here's a sample of my output: (Showing the sort index value of the page)

  • 0
    • 100
    • 170
    • 290
    • 230
    • 200
    • 140
    • 120
    • 30
      • 550
        • 100
        • 100
#208798
Nov 01, 2019 17:06
valdis - Nov 02, 2019 1:28
Please also think about how you can organize your code and avoid ServiceLocator ;)
Travis - Nov 04, 2019 15:33
Thanks for the suggestion valdis. I haven't been on the platform long so I'll need to do some digging on that :P
valdis - Nov 04, 2019 17:24
is method part of your controller? or it's some extension method? or some helper/utility?
Travis - Nov 04, 2019 17:54
Currently the method is part of the controller.
valdis - Nov 04, 2019 17:56
Then you can request required dependency via constructor.
Travis - Nov 04, 2019 19:12
Thanks valdis. I'll try to remember that going forward.
Vote:
 

I guess a Hashtable is not sorted. Use another collection type.

#208799
Nov 01, 2019 17:34
Travis - Nov 01, 2019 20:43
I think you might be right Tomas. I had to rethink my approach, but I managed to get the desired output using a Dictionary.
- Nov 01, 2019 20:52
Nice!
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.