Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 632057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:02:04+00:00 2026-05-13T20:02:04+00:00

I have to sort a namevaluecollection( Items usually 5 to 15 ) Against an

  • 0

I have to sort a namevaluecollection(Items usually 5 to 15) Against an enum(having items more than 60). Right now I am using this extension function, Anyone have better idea to write this code….

    public static NameValueCollection Sort(this NameValueCollection queryString, Type orderByEnumType, bool excludeZeroValues)
    {
        NameValueCollection _processedQueryString = HttpUtility.ParseQueryString("");
        if (queryString.HasKeys())
        {
            SortedList<int, KeyValuePair<string, string>> querySortedList = new SortedList<int, KeyValuePair<string, string>>();
            string[] enumKeys = Enum.GetNames(orderByEnumType);
            int counter = 1000;
            foreach (string key in queryString)
            {
                string value = queryString[key];
                if (enumKeys.Contains(key, StringComparer.CurrentCultureIgnoreCase))
                {
                    int order = (int)Enum.Parse(orderByEnumType, key, true);
                    querySortedList.Add(order, new KeyValuePair<string, string>(key, value));
                }
                else
                {
                    querySortedList.Add(counter, new KeyValuePair<string, string>(key, value));
                    counter++;
                }
            }
            foreach (KeyValuePair<int, KeyValuePair<string, string>> kvp in querySortedList)
            {
                if (!kvp.Value.Value.IsNullOrEmpty() && !kvp.Value.Key.IsNullOrEmpty())
                {
                    if (!excludeZeroValues || kvp.Value.Value != "0")
                    {
                        _processedQueryString.Add(kvp.Value.Key, System.Web.HttpUtility.UrlEncode(kvp.Value.Value));
                    }
                }
            }
        }
        return _processedQueryString;
    }

This works like this

    public enum OrderEnum
    {
        key1=1,
        key2=20,
        key3=3,
        //note
        key4=100,
        key5=2,
        key6=6,
        key7,
        key8,
        key9 
    }
    public void Test()
    {
        NameValueCollection col1 = new NameValueCollection();
        col1.Add("key1", "value1");
        col1.Add("key9", "value1");
        col1.Add("key3", "value1");
        col1.Add("key5", "value1");
        Response.Write(col1.Sort(typeof(OrderEnum)).ToString());
        //out put: key1=value1&key5=value1&key3=value1&key9=value1
    }

This is should also work

public void Test2()
    {
        NameValueCollection col1 = new NameValueCollection();
        col1.Add("key1", "value1");
        col1.Add("key-x", "value1");
        col1.Add("key-y", "value1");
        col1.Add("key9", "value1");
        col1.Add("key3", "value1");
        col1.Add("key5", "value1");
        col1.Add("key-z", "value1");
        Response.Write(col1.Sort(typeof(OrderEnum)).ToString());
        //out put: key1=value1&key5=value1&key3=value1&key9=value1&key-x=value1&key-y=value1&key-z=value1
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:02:04+00:00Added an answer on May 13, 2026 at 8:02 pm

    I think its better to convert your namevaluecollection into List of keyvaluepairs and apply a simple LINQ order by operation, thats quick and simple.

    Add a new extension method to convert your namevaluecoll into list of keyvaluepairs

        public static List<KeyValuePair<string, string>> ToPairs(this System.Collections.Specialized.NameValueCollection collection)
        {
            if (collection == null)
            {
                throw new ArgumentNullException("collection");
            }
    
            return collection.Cast<string>().Select(key => new KeyValuePair<string, string>(key, collection[key])).ToList();
        } 
    

    And just apply the linq order by over this object, something like this

            System.Collections.Specialized.NameValueCollection col1= new System.Collections.Specialized.NameValueCollection();
            col1.Add("key1", "value1");
            col1.Add("key-x", "value2");
            col1.Add("key-y", "value3");
            col1.Add("key9", "value4");
            col1.Add("key3", "value5");
            col1.Add("key5", "value6");
            col1.Add("key-z", "value7"); 
    
            var nvc = col1.ToPairs();
    
            // To order the items based on key in descending order
            var orderedbykey=nvc.OrderByDescending(x => x.Key).ToList();  
    
           // To order the items based on value in descending order           
           var orderedbyval=nvc.OrderByDescending(x => x.Value).ToList();
    
           //or order by ur custom enum key
            var orderbyEnumKeys = colc.OrderBy(x =>
            {
                int en;
                try
                {
                     en = (int)Enum.Parse(typeof(OrderEnum), x.Key);
                }
                catch (Exception ex)
                {
                    return int.MaxValue;
                }
                return en;
            }).ToList();
    

    Hope this helps..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a NameValueCollection and I would like to sort this on his value.
I have this sort of format asp.net MVC View -> Service Layer -> Repository.
I am looking for some help. I have found this script that sort of
I have to sort a list of categories using MySQL as I am using
I have this sort of a link: <a class=image_button data-problem_id=157 style=display: inline; href=#><span>See Solutions
I have to sort this array in descening order with the key likecount. How
I have a sort of menu like this one , but how you can
I've got some troubles with OCMock and UIView. I have sort of this code:
this is the code i have (sort of) foo(a, b) { c = a.item;
I have read Sort NSArray using sortedArrayUsingFunction and it's possible that the following question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.