Click here to Skip to main content
15,897,315 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi there,

I have a list of values

200000
90000
500000
60000
150000

I need to sort it

60000
90000
150000
200000
500000

I am using an ArrayList Sort and it sorting it as

150000
200000
500000
60000
90000

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);
                }
            }
        }


Any help would do please
Posted

C#
int[] intArray = { 8, 10, 2, 6, 3 };

// Sort your int array
Array.Sort(intArray);
foreach (var i in intArray)
{
    Console.WriteLine(i);
}

// Sort it the other way
foreach (var i in intArray.Reverse())
{
    Console.WriteLine(i);
}


order objects by property:

C#
MyCollection.OrderBy(o => o.PropertyName);
MyCollection.OrderByDescending(o => o.PropertyName);
 
Share this answer
 
C#
var sortedList = list.Cast<string>().OrderBy(item => int.Parse(item));


Check this.

http://stackoverflow.com/questions/852439/how-to-sort-elements-of-array-list-in-c-sharp[^]
 
Share this answer
 
v2
 
Share this answer
 
Comments
kornakar 16-Aug-12 5:46am    
Funny that my answer is downvoted, but it is exactly the same link as in Santhosh Kumar J's answer :)
DJAppleboy 23-Aug-12 6:47am    
Maybe because he posted it first...lol and I still couldn't get it to work
kornakar 24-Aug-12 2:20am    
Well I posted it first (Solution 1, see) :) But I guess I should have provided a full code for him and not the link only.
You are sorting string values, that is why your sorting does not look normal.

Use the following line when adding data :
C#
text_list.Add(long.Parse(li.Text));
 
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