2

How can I sort the Arraylist in ascending and descending orders. Example.

ArrayList list= new ArrayList();
list.Add(2);
list.Add(8);
list.Add(0);
list.Add(1);

How can I sort the above list in both ascending and descending order?

4
  • That's not valid C# code (Add, not add). Why are you using the non-generic ArrayList type anyway? Are you using .NET 1.1 for some reason? Commented Jul 14, 2011 at 14:59
  • Ya. list.add(). that is wrong only sorry for that.I want to sort the arraylist since i'm dynamically adding the values in codebehind.Can u suggest someother collection which supports sorting Commented Jul 14, 2011 at 15:02
  • No, you mean list.Add. ArrayList has always been sortable, but without knowing why you're using a non-generic type rather than the rather nice List<T> it's hard to advise you well. Commented Jul 14, 2011 at 15:05
  • Another question is, probably: what type of sort do you expect? Since each element is an object, do you want them compared as strings, or numerically? Commented Jul 14, 2011 at 15:19

8 Answers 8

2

You can use list.Sort() for ascending order. For descending order, you need to reverse the order by implementing IComparer. Something like this will do:

// calling normal sort:
ArrayList ascendingList = list.Sort();

// calling reverse sort:
ArrayList descendingList = list.Sort(new ReverseSort());

// implementation:
public class ReverseSort : IComparer
{
    public int Compare(object x, object y)
    {
        // reverse the arguments
        return Comparer.Default.Compare(y, x);
    }

}

Note that, like Jon Skeet mentions in the comment thread under the main question, you do not need to use the untyped ArrayList at all. Instead, you can use the generic List<T>, which is typesafe and is simply more versatile.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use list.Sort() function. see example here http://msdn.microsoft.com/ru-ru/library/0e743hdt.aspx

Comments

1

You can you use ArrayList.Sort Method

Comments

1

ArrayList contains a Sort method. (Edited to remove incorrect .NET information)

3 Comments

Can you explain me briefly or can u provide me any link
See this page for documentation of the ArrayList class. msdn.microsoft.com/en-us/library/… There you can find information on the Sort method. You will have to implement a comparator to sort in ASC vs DSC, however examples are provided in the documentation of this.
It's contained a Sort method since 1.0 - certainly in 1.1: msdn.microsoft.com/en-us/library/895sadkk(v=VS.71).aspx
0

Using an array list instead of List<int> here is dangerous, however, here is what you are looking for:

System.Collections.ArrayList al = new System.Collections.ArrayList();
al.Add(5);
al.Add(3);
al.Add(1);
al.Add(4);
al.Add(0);
al.Sort();

foreach (var x in al)
{
    Console.WriteLine(x.ToString());
}

al.Sort(new DescendingIntSorter());

foreach (var x in al)
{
    Console.WriteLine(x.ToString());
}



public class DescendingIntSorter : System.Collections.IComparer
{
    public int Compare(object x, object y)
    {
        return ((int)y).CompareTo((int)x);
    }
}

Comments

0

list.Sort() will sort them but I would suggest using List<> and Collections for sorting. Avoid using ArrayList imo.

Comments

0

With the ascending list, you can use list.Sort()

And the for the descending list, there's an easy way to do it!

You can do descending lists like this:

list.Sort();
list.Reverse();

Comments

0
    private void DisplayMain()
    {
        string sValue = "1,2,5,8,6,7,110,220";
        txtValue.Text = sValue;
        string[] asDS = sValue.Split(',');
        ArrayList alData = new ArrayList();
        foreach (string sDS in asDS)
        {
            alData.Add(sDS);
        }//end of foreach
        alData.Sort(new IntSort());
        //============
        int iCnt = 0;
        StringBuilder sbResult = new StringBuilder();
        foreach (string sData in alData)
        {
            iCnt++;
            sbResult.AppendLine(iCnt.ToString() + " ====> " + sData);
        }//end of if 
        string sResult = sbResult.ToString();
        txtResult.Text = sResult;
    }//end of DisplayMain

    public class IntSort : IComparer
    {
        public int Compare(object x, object y)
        {
            string sX = x.ToString();
            string sY = y.ToString();
            int iResult = 0;
            int iX = int.Parse(sX);
            int iY = int.Parse(sY);
            if(iX > iY) { iResult = 1; }//end of if
            else if (iY > iX) { iResult = -1; }//end of if 
            return iResult;
            //return ((int)y).CompareTo((int)x);
        }
    }

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.