Click here to Skip to main content
15,895,798 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
find out the 5 largest values in array
Posted

use LINQ
C#
int[] anArray = { 1, 5, 2, 7 ,8,3,6,9};
int[] max5items = anArray.OrderByDescending(x=>x).Take(5).ToArray();
 
Share this answer
 
Comments
BillWoodruff 26-Dec-14 6:02am    
+5
DamithSL 26-Dec-14 6:03am    
thank you, Bill
deepankarbhatnagar 26-Dec-14 6:27am    
Nice
sad smile 26-Dec-14 11:27am    
thank you ,but I want to get the 5 values without sorting
Just use Array.Sort method which will sort all the values in the array in ascending order then fetch first 5 values.
 
Share this answer
 
Comments
sad smile 26-Dec-14 11:19am    
thank you for your care ,but I want to get the 5 values without sorting .
You can sort your array using Sort function and pick up first 5 elements:

Code to sort your Array:

C#
int[] intArray = new int[5] { 8, 10, 2, 6, 3 };
Array.Sort(intArray);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900