[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

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

How can you sort the elements of the array in descending order?
0
chuang4630
Asked:
chuang4630
  • 2
1 Solution
 
InteractiveMindCommented:
What data type is the array?
String, int, .. ?
0
 
InteractiveMindCommented:
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
0
 
pallospCommented:
You can call Array.Sort(i) then Array.Reverse(i).
It might be slightly faster than a custom comparer function.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now