How to Sort an ArrayList of Type Strings By One With Longest Length Being First to Shortest Length Being Last Using C#

Posted on 2011-10-27
Last Modified: 2013-12-16
I am attempting to sort an ArrayList of strings, and I was wondering if someone could help me find the sytanx to maybe use the ArrayList.Sort() function, if it will sort all of the string objects within the ArrayList from the longest length to the shortest length.

Any help with this issue would be greatly appreciated.
Question by:thenthorn1010
    1 Comment
    LVL 85

    Accepted Solution

    *I recommend the newer List<> instead of the ArrayList*

    If you insist, though, try something like this:
    public partial class Form1 : Form
            private void button1_Click(object sender, EventArgs e)
                ArrayList al = new ArrayList();
                MyComparer mc = new MyComparer();
                foreach (Object x in al)
        public class MyComparer : IComparer
            int IComparer.Compare(object x, object y)
                return ((string)y).Length.CompareTo(((string)x).Length);

    Open in new window


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now