Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • trfletch 598 posts 604 karma points
    Jul 27, 2011 @ 10:46
    trfletch
    0

    Sort nodes alphabetically in C#

    Hi all,

    I have an Umbraco 4.7 site and have created a search form usercontrol, part of the form has a dropdown called "Location" which lists child nodes of a specific node and then child nodes of that as follows:

    Kent
      - Maidstone
      - Rochester

    Cornwall
      - Newquay

    All works well except I want the dropdown list to be sorted in alphabetical order. Can someone please help me do this with my C# code below, many thanks:

     

     

    private void PopulateLocation(string selectedValue)

    {

     

     

    if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["LettingSearch_Lettings_NodeId"]))

    {

     

     

    var specificNode = new Node(Convert.ToInt32(ConfigurationManager.AppSettings["LettingSearch_Lettings_NodeId"]));

     

     

    var childNodes = specificNode.Children;

     

    ddlLocation.Items.Add(

     

    new ListItem("Any location", "0"));

     

    foreach (Node node in childNodes)

    {

     

    ddlLocation.Items.Add(

     

    new ListItem(node.Name.ToString(), node.Id.ToString()));

     

     

    var subChildNodes = node.Children;

     

     

    foreach (Node subNode in subChildNodes)

    {

    ddlLocation.Items.Add(

     

    new ListItem(Server.HtmlDecode("  - ") + subNode.Name.ToString(), subNode.Id.ToString()));

    }

    }

     

     

    if (!String.IsNullOrEmpty(selectedValue))

    {

    ddlLocation.Items.FindByValue(selectedValue).Selected =

     

    true;

    }

    }

    }

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Jul 27, 2011 @ 11:34
    Stefan Kip
    0

    Change

    foreach(Node node in childNodes)

    to

    foreach(Node node in childNodes.OrderBy(n => n.CreateDate))

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Jul 27, 2011 @ 11:35
    Stefan Kip
    1

    Err sorry, I mean:

    childNodes.OfType<Node>().OrderBy(n => n.Name)
  • trfletch 598 posts 604 karma points
    Jul 27, 2011 @ 11:47
    trfletch
    0

    Hi,

    Thanks for the quick response, I tried what you said but it is showing errors in visual studio, this is what I have put:

    foreach

     

    (childNodes.OfType<Node

    >().OrderBy(n => n.Name))

     

    I am getting "Identifier expected" with regards to"()"

    I am also getting "Invalid expression term ')'"

    and finally ") expected" regarding the last ")"

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Jul 27, 2011 @ 12:31
    Stefan Kip
    0

    This should work though...

    foreach(Node node in childNodes.OfType<Node>().OrderBy(n => n.Name))

    {

     

    }

  • trfletch 598 posts 604 karma points
    Jul 27, 2011 @ 14:17
    trfletch
    0

    Excellent, that works perfectly, thank you very much

Please Sign in or register to post replies

Write your reply to:

Draft