4

I'm trying to sort an ArrayList of String.

Given:

{A,C,AA,B,CC,BB}

Arraylist.Sort gives:

{A,AA,B,BB,C,CC}

What I need is:

{A,B,C,AA,BB,CC}
3
  • 1
    First split the list in seperat list depending on length and then sort them individually. Commented Jan 10, 2013 at 14:38
  • 1
    Is there a reason for not using a strongly typed List<string>? Commented Jan 10, 2013 at 14:47
  • 1
    There are speed advantages of using an ArrayList over a LinkedList. Commented Jan 10, 2013 at 15:03

5 Answers 5

12
ArrayList list = new ArrayList {"A","C","AA","B","CC","BB"};

var sorted = list.Cast<string>()
                 .OrderBy(str => str.Length)
                 .ThenBy(str => str);

//LinqPad specific print call
sorted.Dump();

prints:

A 
B 
C 
AA 
BB 
CC 
Sign up to request clarification or add additional context in comments.

2 Comments

Just note that this doesn't modify the ArrayList, it creates a new sequence of the values from that collection. (It may be acceptable, it's just important to be aware of that.)
yes, sure, good note. Another note that even if you modify list after sorted variable has been declared, but before actually called - than it would contain new element. sorted query is being executed in deferred manner
4

It's easier to do this with Linq as so:

string [] list = { "A","C","AA","B","CC","BB"};

var sorted = list.OrderBy(x=>x.Length).ThenBy(x=>x);

Note that the OrderBy method returns a new list. If you want to modify the original, then you need to re-assign it as so:

list = list.OrderBy(x=>x.Length).ThenBy(x=>x).ToArray();

Comments

0

This is kind of old school but, I went the IComparer Interface . . .

public class SortAlphabetLength : System.Collections.IComparer
{
    public int Compare(Object x, Object y)
    {
        if (x.ToString().Length == y.ToString().Length)
            return string.Compare(x.ToString(), y.ToString());
        else if (x.ToString().Length > y.ToString().Length)
            return 1;
        else
            return -1;
    }
}

and then test it . . .

class Program
{
    static void Main(string[] args)
    {
        ArrayList values = new ArrayList()
        {
            "A","AA","B","BB","C","CC"
        };

        SortAlphabetLength alphaLen = new SortAlphabetLength();
        values.Sort(alphaLen);

        foreach (string itm in values)
            Console.WriteLine(itm);
    }
}

output:

A
B
C
AA
BB
CC

7 Comments

Now put a null value in that collection and see what happens.
I'm sorry I didn't see that in the question. I'm just trying to answer the question. Thanks for pointing that out though.
If you're writing code to only display the sorted values of that one example than you may as well just print out A, B C, ... It's example input, not the only input he'll ever have to put into it.
Thanks for nitpicking. Wise, Sir.
Yeah, robust code that works is overrated; it's much better to use code that's longer, more complex, is harder to understand, and occasionally crashes when there is a sensible course of action.
|
0

I would suggest using the ToArray() method (or just using a List<string> instad of an ArrayList) to take advantage of the OrderBy and ThenBy functions. It would look something like this:

list = list.OrderBy(/*Order it by length*/).ThenBy(/*Order alphabetically*/);

Comments

0

You could create an IComparable class taking in two Strings and sort them as follows:

if (a.Length == b.Length)
    return String.Compare(a, b);
return a.Length.CompareTo(b.Length);

You may also want to handle null cases.

Comments

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.