Link to home
Start Free TrialLog in
Avatar of worldfear
worldfear

asked on

c# linq sort ienumberable enums

how do i use orderby on this:
Market m in Enum.GetValues(typeof(Market)).Cast<Market>()
??
so when i do this
foreach(Market m in Enum.GetValues(typeof(Market)).Cast<Market>())
            {
                DropDownList2.Items.Add(new ListItem(MarketUtil.GetDisplayString(m), "0"));
            }

the items are ordered?
Avatar of Jarrod
Jarrod
Flag of South Africa image

try

var marketValues = ((Market[]) Enum.GetValues(typeof (Market))).ToList();

            marketValues.Sort();
            foreach (var mValue in marketValues)
            {
                DropDownList2.Items.Add(new ListItem(MarketUtil.GetDisplayString(m), "0"));
            }

Open in new window

Try changing:
            marketValues.Sort();
            foreach (var mValue in marketValues)
 
to:
            foreach (var mValue in marketValues.Sort())
 
Otherwise, the .Sort() is not doing anything.
Gary Davis
Avatar of worldfear
worldfear

ASKER

that didn't sort them - the order was the same.  

an added complexity, i need the value of the enum as well (the int)

protected void DropDownList2_Init(object sender, EventArgs e)
        {
            //how do i sort these?
            foreach(Market m in Enum.GetValues(typeof(Market)).Cast<Market>())
            {
                DropDownList2.Items.Add(new ListItem(MarketUtil.GetDisplayString(m), ((int)m).ToString()));
            }
        }
gardavis
for your suggestion - error
foreach cannot operate on variables of type void bc void isnt enumberable
ASKER CERTIFIED SOLUTION
Avatar of Jarrod
Jarrod
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hey thanks i'll let you know monday!