The Sort method of the List class sorts everything the default way : ascending.
You can use the Reverse method of this class to get what you want or you could implement the IComparer interface like in this snippet.
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
private class SortIntDescending : IComparer
{
int IComparer
{ if (a > b) return -1; //normally greater than = 1
if (a < b) return 1; // normally smaller than = -1
else return 0; // equal
} }
static List
static void Main(string[] args)
{
intlist.Add(5); //fill the list with 5 ints
intlist.Add(3);
intlist.Add(5);
intlist.Add(15);
intlist.Add(7);
Console.WriteLine("Unsorted list :");
Printlist(intlist); Console.WriteLine();
// intlist.Sort(); uses the default Comparer, which is ascending
intlist.Sort(new SortIntDescending()); //sort descending
Console.WriteLine("Sorted descending list :");
Printlist(intlist);
Console.ReadKey(); //wait for keydown
}
static void Printlist(List
{
foreach (int i in L) //print on the console
{
Console.WriteLine(i);
} } }}
We collect videos & images from online sites like youtube and some other websites which provide them. And most of the News is also gathered from other online websites. Any material downloaded or otherwise obtained through the use of the service is done at your own discretion and risk and that you will be solely responsible for any damage to your computer system or loss of data that results from the download of any such material.
If you feel that the content on the site is illegal or Privacy please contact us at srinuchalla@gmail.com and such videos, images or any Content will be removed with immediate effect.
No comments:
Post a Comment