[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1445
  • Last Modified:

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?
0
worldfear
Asked:
worldfear
  • 3
  • 2
1 Solution
 
zadeveloperCommented:
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

0
 
Gary DavisDir Internet SvcsCommented:
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
0
 
worldfearAuthor Commented:
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()));
            }
        }
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
worldfearAuthor Commented:
gardavis
for your suggestion - error
foreach cannot operate on variables of type void bc void isnt enumberable
0
 
zadeveloperCommented:
try this

var marketValues = ((Market[]) Enum.GetValues(typeof (Market))).ToList();
            foreach (var m in new List<Market>(marketValues.OrderBy(p => p.ToString())))
            {
                DropDownList2.Items.Add(new ListItem(MarketUtil.GetDisplayString(m), ((int)m).ToString()));
            } 

Open in new window

0
 
worldfearAuthor Commented:
hey thanks i'll let you know monday!
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now