This blog retrieves top tree record from an
array and a list by using following three steps
- Sorting the array and list
- Reverseing the array and list
- Calling the Take function
some functions are listed for different class
used to retrieve list.
Function for array
- Sort
- Revers
Function for List
- Sort
- Revers
Function in Linq
- Take
If you are want to retrieves top 3 records
from array, use above mention two functions for array and take functions
linq or if you are want to retrieves record from list call the functions
mentions for list and take function.
see the below examples
For array
foreach (int ind in integers)
{
Console.WriteLine(ind);
}
int[]
intigerarray = new
int[] { 6, 2, 9, 4, 5, 1, 7, 8, 3 };
Array.Sort(intigerarray);
Array.Reverse(intigerarray);
foreach (int ind in integers)
{
Console.WriteLine(ind);
}
Output
9
8
7
For List
integerList.Reverse();
foreach (int ind in integerList.Take(3))
{
Console.WriteLine(ind);
}
List<int>
integerList = new List<int>();
integerList.Add(6);
integerList.Add(2);
integerList.Add(9);
integerList.Add(4);
integerList.Add(5);
integerList.Add(1);
integerList.Add(7);
integerList.Add(8);
integerList.Reverse();
foreach (int ind in integerList.Take(3))
{
Console.WriteLine(ind);
}
Output
9
8
7
Tausif AlamPosted Oct 8, 2013, 1:59 AM
Thanks a lot Kailash for this simple yet effective article. Keep it up :)