Click here to Skip to main content
16,015,003 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm sorting a list of string values but I'm not getting the expected results. I've been on Google and wrapped my head around with no luck. Any help would do please.

public void OrderDropDowns(ref DropDownList objDDL)
        {
            // ArrayList to keep text and value items within the specified Drop Down
            ArrayList text_list = new ArrayList();
            ArrayList value_list = new ArrayList();
 
            foreach (ListItem li in objDDL.Items)
            {
                if (li.Text != "-Select One-")
                {
                    text_list.Add(li.Text);
                }
            }
            if (text_list.Count >= 8)
            {
                text_list.Sort(0, 4, null);
            }
            else
            {
                text_list.Sort();
            }
 
            foreach (object li in text_list)
            {
                string value = objDDL.Items.FindByText(li.ToString()).Value;
                value_list.Add(value);
            }
 
            // Adding Sorted Items to the Drop Down List
            objDDL.Items.Clear();
            objDDL.Items.Add("-Select One-");
            for (int i = 0; i < text_list.Count; i++)
            {
                if (i != text_list.Count)
                {
                    ListItem objItem = new ListItem(text_list[i].ToString(), value_list[i].ToString());
                    objDDL.Items.Add(objItem);
                }
            }
        }


List of values
Ex 1 Yr 200000
Ex 2 Yr 50000
Ex 1 Yr 60000
Ex 2 Yr 150000

I need to sort it
Ex 1 Yr 60000
Ex 1 Yr 200000
Ex 2 Yr 50000
Ex 2 Yr 150000

I am using an ArrayList Sort and it sorting it as
Ex 1 Yr 200000
Ex 1 Yr 60000
Ex 2 Yr 50000
Ex 2 Yr 150000
Posted

Never use ArrayList. Use the generic classes like List<string>. In addition, you'd do better to make this a struct. You want to sort based on the numeric values starting at position 9 in your string. If you keep them as a string, you need to write a custom sort method to do this. Here[^] is an article on custom sorting. You want to compare the whole string first, THEN if the first sections are the same, parse out the number and compare it numerically.
 
Share this answer
 
Use the alternate sorting method, by passing an IComparer implementation. See MSDN[^].

It is not a good approach to use two separated collections. Better create a class and use a generic list (http://en.csharp-online.net/CSharp_Generics_Recipes%E2%80%94Replacing_the_ArrayList_with_Its_Generic_Counterpart[^]), or a SortedList[^].
 
Share this answer
 
v3
I hope you are aware that the when one is sorting strings, the output you are experiencing is exactly what is to be expected.

The String "Ex 1 Yr 200000" is less than "Ex 1 Yr 60000". The proper term that governs this behaviour is called Collating Sequence[^].
From what you are expecting, you should actually pick apart your string into an appropriate type you will have to define, being careful to implement the IComparable[^] interface. This will let you control the way in which the elements will be sorted. When done with the sort you can reassemble the text strings in order to put them into your dropdown list.

Regards,

— Manfred
 
Share this answer
 
v2
I've created four ArrayList (one for each year) and one for each numerical value corresponding to a year.

Than I rip all the contents and place them in their appropriate ArrayList. Than I loaded the ArrayList Items in the order I wanted them to be.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900