top 5 on the list
top 5 on the list
22 Oct 2018, 06:29
Dear Panagiotis!, Hi
Let us assume there is a aliset numbers {1,5,6,5,1,6,8,5,2,1,3,5}
How do find indexes of the top 6 numbers?
Regards
Alexander
Replies
alexander.n.fedorov
22 Oct 2018, 10:41
I do not want it sorted, then I loose important information
Maybe I can circle them or use LINWQ?
I just do not know how
But I do not want to change the original structure of the list
@alexander.n.fedorov
PanagiotisCharalampous
22 Oct 2018, 11:19
Hi Sasha,
OrderBy will not sort the original list/dictionary. It will return a new sorted instance.
Best Regards,
Panagiotis
@PanagiotisCharalampous
alexander.n.fedorov
22 Oct 2018, 14:22
RE:
Panagiotis Charalampous said:
Hi Sasha,
OrderBy will not sort the original list/dictionary. It will return a new sorted instance.
Best Regards,
Panagiotis
Unfortunately, I do not know how to do that, so I guess I will have to find a book and study
@alexander.n.fedorov
PanagiotisCharalampous
22 Oct 2018, 14:33
Here is an example
var dictionary = new Dictionary<int, int>(); dictionary.Add(1, 3); dictionary.Add(2, 5); dictionary.Add(3, 1); dictionary.Add(4, 2); dictionary.Add(5, 9); dictionary.Add(6, 4); dictionary.Add(7, 7); dictionary.Add(8, 6); dictionary.Add(9, 10); dictionary.Add(10, 8); foreach (var v in dictionary.OrderBy(x => x.Value).Take(5)) { Print("Index: " + v.Key + " Value: " + v.Value); }
Best Regards,
Panagiotis
@PanagiotisCharalampous
alexander.n.fedorov
22 Oct 2018, 14:43
RE:
Panagiotis Charalampous said:
Here is an example
var dictionary = new Dictionary<int, int>(); dictionary.Add(1, 3); dictionary.Add(2, 5); dictionary.Add(3, 1); dictionary.Add(4, 2); dictionary.Add(5, 9); dictionary.Add(6, 4); dictionary.Add(7, 7); dictionary.Add(8, 6); dictionary.Add(9, 10); dictionary.Add(10, 8); foreach (var v in dictionary.OrderBy(x => x.Value).Take(5)) { Print("Index: " + v.Key + " Value: " + v.Value); }Best Regards,
Panagiotis
Thank you Panagiotis, I will try to think of it
Regards
@alexander.n.fedorov
alexander.n.fedorov
22 Oct 2018, 15:09
So, Dictionary is like 2 dimensional (kind of) list and you would do
"
for (int i=0.........)
{
dictionary.Add(i+1, numbers[i]
}
?
@alexander.n.fedorov
alexander.n.fedorov
22 Oct 2018, 15:09
for (int i=0.........)
{
dictionary.Add(i+1, numbers[i])
}
?
@alexander.n.fedorov
alexander.n.fedorov
22 Oct 2018, 16:27
RE:
alexander.n.fedorov said:
So, Dictionary is like 2 dimensional (kind of) list and you would do
"
for (int i=0.........)
{
dictionary.Add(i+1, numbers[i]
}
?
Thanks, Panagiotis, it helped a lot!
@alexander.n.fedorov
PanagiotisCharalampous
22 Oct 2018, 10:38
Hi Sasha,
You can use the following tools
1. Dictionary. Add your entries as values and your index as keys.
2, Sort function. Sort your dictionary using the keys.
3. Take function. Take the top x entries of the sorted dictionary.
Let me know if the above helps.
Best Regards,
Panagiotis
@PanagiotisCharalampous