Aug 14
How do you sort a C# array in descending or reverse order? A simple way is to sort the array in ascending order, then reverse it:
int[] array = new int[] { 3, 1, 4, 5, 2 }; Array.Sort<int>( array ); Array.Reverse( array );
Of course, this is not efficient for large arrays.
A better approach is to create a custom Comparer. Following is a nice generics class that will sort an array in descending order. Note that the object type must be comparable (inherit from IComparable) as any useful class should.
static public class ArraySorter<T> where T : IComparable { static public void SortDescending( T[] array ) { Array.Sort<T>( array, s_Comparer ); } static private ReverseComparer s_Comparer = new ReverseComparer(); private class ReverseComparer : IComparer<T> { public int Compare( T object1, T object2 ) { return -((IComparable)object1).CompareTo( object2 ); } } }
Here’s a simple console program to test it:
using System; using System.Collections.Generic; namespace CSharp411 { class Program { static void Main( string[] args ) { int[] array = new int[] { 3, 1, 4, 5, 2 }; ArraySorter<int>.SortDescending( array ); WriteArray( array ); Console.ReadLine(); } static private void WriteArray( int[] array ) { foreach (int i in array) { Console.WriteLine( i ); } } } static public class ArraySorter<T> where T : IComparable { static public void SortDescending( T[] array ) { Array.Sort<T>( array, s_Comparer ); } static private ReverseComparer s_Comparer = new ReverseComparer(); private class ReverseComparer : IComparer<T> { public int Compare( T object1, T object2 ) { return -((IComparable)object1).CompareTo( object2 ); } } } }
This artical is very useful for me. I am a .NET developer and search for generic list sort decending.
Thanks
danks
Why reinvent the wheel … just use List and List.Reverse
hi,
First of all. Thanks very much for your useful post.
I just came across your blog and wanted to drop you a note telling you how impressed I was with the information you have posted here.
Please let me introduce you some info related to this post and I hope that it is useful for .Net community.
There is a good C# resource site, Have alook
http://www.csharptalk.com/2009/09/c-array.html
http://www.csharptalk.com/2009/10/creating-arrays.html
simi
thanks. for this information about this program in C# …………………………
It’s really a big help! Thanks author..
it didn’t work
A simple implementation using delegates:
int[] intArray = new int[] { 8, 10, 2, 6, 3 };
Array.Sort(intArray, delegate(int a, int b) { return b.CompareTo(a); });
Thanks and Regards
Nishant Rana
for (int i = 0; i < c.Length; i++)
{
Console.Write("Enter a Place [{0}]: ", i);
c[i] =int.Parse( Console.ReadLine());
{
foreach (int value in array);
}
Array.Reverse(array);
}
public void displayPlaces(int[] c)
{
for (int i=0;i<c.Length;i++)
{
Console.WriteLine("Places [{0}]:{1}", i, c[i]);
foreach (string str in strArray)
Console.Write(str + " ");
}
help me to sort it
Thaaaanks.. I want to learn Genenrics
using System;
using System.Linq;
class Program
{
static void Main()
{
//
// An integer array that is in an arbitrary order.
//
int[] array = { 1, 3, 5, 7, 9 };
//
// Select the elements in a descending order with query clauses.
//
var result = from element in array
orderby element descending
select element;
//
// Evaluate the query and display the results.
//
foreach (var element in result)
{
Console.WriteLine(element);
}
}
}
hi am new for c# HOW TO SORT SUBJECT AND MARKS FOE EX C# 75 MATHS 50 MEANS DEN OUTPUT WILL BE MATHS 50 AND C# 75
HOW TO SORT PLS HELP ME
Thnks!