C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




How to Sort a string Array in Descending order


Posted Date: 07 Nov 2007    Resource Type: Code Snippets    Category: String Manipulations

Posted By: Ravi Kumar       Member Level: Silver
Rating:     Points: 6



This code sample shows you how to sort a string array in descending order. Since there is no method for sorting an array in descending order, I have done a small trick. No rocket science though!



class players
{
static void Main(string[] args)
{
string[] players = new string[] { "Pete Sampras", "John McEnroe", "Jimmy Connors", "Roy Emerson", "Rod Laver" };

//display elements of the array
Console.WriteLine("Elements of String array");
foreach (string str in players)
Console.Write(str + ", ");

Console.WriteLine();


//display elements of the array in sorted order
Console.WriteLine("Elements of String array (sorted)");
Array.Sort(players);


foreach (string str in players)
Console.Write(str + ", ");

Console.WriteLine();
Console.WriteLine("Elements of String array (in descending order)");
Array.Sort(players);
Array.Reverse(players);


foreach (string str in players)
Console.Write(str + ", ");


}
}


The trick used was to sort an array and then reverse it (the array). Thus, we have an array sorted in the descending order.

OUTPUT:




Elements of String array
Pete Sampras, John McEnroe, Jimmy Connors, Roy Emerson, Rod Laver,
Elements of String array (sorted)
Jimmy Connors, John McEnroe, Pete Sampras, Rod Laver, Roy Emerson,
Elements of String array (in descending order)
Roy Emerson, Rod Laver, Pete Sampras, John McEnroe, Jimmy Connors,





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Using Array.Sort method  .  Using Array.Reverse method  .  Sort an Array in Descending Order  .  Sort an Array  .  Sort a String Array in Descending order  .  Sort a String Array  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Convert text to Propertcase
Previous Resource: How to do String Manipulations in c#
Return to Discussion Resource Index
Post New Resource
Category: String Manipulations


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use