2

I'm curious as to the best route (more looking towards simplicity, not speed or efficiency) to sort a DropDownList in C#/ASP.NET - I've looked at a few recommendations but they aren't clicking well with me. this drop down is giving me list in alphabetical order. But I have to sort out randomly.

Note:I do not have control over how the data comes into the DropDownList - I cannot modify the SQL.

public void populateLocationList()
{
    DataTable dt_locations = (DataTable)daa_addresses.GetDataByEventLocations(int_eventid);

    if (dt_locations != null && dt_locations.Rows.Count > 0)
    {
        // Populate locations dropdown menu
        // Bind locationsList instead of dt_locations
        ddl_locations.DataTextField = "Key";
        ddl_locations.DataValueField = "Value";
        ddl_locations.DataSource = RemoveDuplicateLocations(dt_locations);
        ddl_locations.DataBind();



        string location = "";

        // Set the selection based upon the query string
        if (Request.QueryString["loc"] != null)
        {
            location = Request.QueryString["loc"];
            locationLbl.Text = Request.QueryString["loc"];
            locationID.Value = "-1";
        }

        if (dt_locations.Rows.Count == 1)
        {
            location = ddl_locations.Items[0].Text;
            locationLbl.Text = location;
        }

        // Set location in drop down list
        int int_foundlocation = 0;
        bool foundLocation = false;

        foreach (ListItem lsi_item in ddl_locations.Items)
        {
            if (lsi_item.Text.ToLower().Trim() == location.ToLower().Trim())
            {
                int_foundlocation = ddl_locations.Items.IndexOf(lsi_item);
                foundLocation = true;
                break;
            }
        }

        ddl_locations.SelectedIndex = int_foundlocation;

        if (ddl_locations.Items.Count == 1)
        {
            // Make location label visible.
            locationLbl.Visible = true;
            ddl_locations.Visible = false;
        }
        else
        {
            locationLbl.Visible = false;
            ddl_locations.Visible = true;
        }
        //* defualt location S for short courses *//
        if (!IsPostBack && !foundLocation)
        {
            ListItem s = ddl_locations.Items.FindByText("S");
            int index = 0;

            if (s != null)
            {
                index = ddl_locations.Items.IndexOf(s);
            }

            ddl_locations.SelectedIndex = index;
            ddl_locations.DataBind();
        }
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
sunny
  • 17
  • 5
  • 1
    I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Dec 04 '12 at 00:51
  • 3
    Off-topic: John, do you have the markdown for that permanently in your clipboard? lol. – Simon Whitehead Dec 04 '12 at 00:53
  • Similar question already asked see here -- http://stackoverflow.com/questions/222572/sorting-a-dropdownlist-c-asp-net – Ant Dec 04 '12 at 09:34

1 Answers1

0

I have to sort out randomly.

You'd have to shuffle rows, probably with something close to this code (borrowed from @configurator's answer):

internal static class Extensions
{
    internal static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random rng)
    {
        T[] elements = source.ToArray();
        // Note i > 0 to avoid final pointless iteration
        for (int i = elements.Length - 1; i > 0; i--)
        {
            // Swap element "i" with a random earlier element it (or itself)
            int swapIndex = rng.Next(i + 1);
            yield return elements[swapIndex];
            elements[swapIndex] = elements[i];
            // we don't actually perform the swap, we can forget about the
            // swapped element because we already returned it.
        }

        // there is one item remaining that was not returned - we return it now
        yield return elements[0]; 
    }
}

Assuming that RemoveDuplicateLocations returns a DataTable the binding part of your code should be changed to:

        ddl_locations.DataSource = RemoveDuplicateLocations(dt_locations)
            .AsEnumerable()
            .Shuffle(new Random())
            .CopyToDataTable();
Community
  • 1
  • 1
Serge Belov
  • 5,633
  • 1
  • 31
  • 40