How can you sort the elements of the array in descending order?

Posted on 2006-05-04
Last Modified: 2010-04-16
How can you sort the elements of the array in descending order?
Question by:chuang4630
    LVL 25

    Expert Comment

    What data type is the array?
    String, int, .. ?
    LVL 25

    Expert Comment

    An example for int's; it's the same process for all other data types though (just fiddle with the Compare() method as necessary):

    public class DescendingOrder : IComparer
        int IComparer.Compare( Object x, Object y )
            return -( (Integer)x - (Integer)y ) ;

    int [] i = new int [] { 1, 56, 234, 53, 56 ,67, 67, 34 } ;  // e.g.

    Array.sort( i, new DescendingOrder() ) ;  // sort here
    LVL 9

    Accepted Solution

    You can call Array.Sort(i) then Array.Reverse(i).
    It might be slightly faster than a custom comparer function.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    758 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

    9 Experts available now in Live!

    Get 1:1 Help Now