如何根据数值将数字分为第1,第2等 [英] How to grade numbers as 1st, 2nd etc based on their values
本文介绍了如何根据数值将数字分为第1,第2等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好。
我试图在包含两列的数据网格中对一组数字进行评级。我希望Coulmn B的单元格具有第1,第2,第3等值,基于A列的值。
例如:
Col A Col B
80 2
305 1
23 5
43 4
31 3
我的尝试:
我所知道的是如何将结果分配给B列后找到
Dim iRow As Integer
Dim cellA As Integer
Dim cellB As Integer
iRow = 0 To Me.DataGridView1。 RowCount - 1
Me.DataGridView1.Rows(iRow).Cells(1).Value = cellA
Next
iRow = irow + 1
解决方案
这样的东西应该可以正常排序:
Dim cumulativeRank As整数 = 1
对于 < span class =code-keyword>每个组 As IGrouping( of 对象,DataGridViewRow)在 DataGridView1.Rows.Cast( Of DataGridViewRow)()。GroupBy(功能(r)r.Cells( 0 )。值)。 OrderByDescending( Function (g)g.Key)
Dim rank 作为 整数 = cumulativeRank
对于 每个行作为 DataGridViewRow 在组中
row.Cells ( 1 )。值=排名
cumulativeRank + = 1
下一步
下一页
如果您不关心领带,那么您不需要分组:
Dim rank As 整数 = 1
对于 每个行 As DataGridViewRow 在 DataGridView1.Rows.Cast( DataGridViewRow)()。OrderByDescending( Function (r)r.Cells( 0 )。值)
row.Cells( 1 )。值= rank
rank + = 1
下一步
我建议使用字典 [ c $ c> ColA
Dim dgv As DataGridView = 我 .DataGridView1
Dim oDict As Dictionary( Of 整数,整数)= dgv.Rows()。Cast( of DataGridViewRow)_
.OrderByDescending( Function (r)r.Cells( ColA)。Value)_
.GroupBy( Function (r)r.Cells( ColA)。值)_
。选择(功能(grp ,index)新 KeyValuePair( 整数 ,整数)(grp.Key,index + 1))_
.ToDictionary(功能 (kvp)kvp.Key,功能(kvp)kvp.Value)
用于 每个 dgr As DataGridViewRow 在 dgv.Rows
dgr.Cells( 1 )。值= oDict(dgr.Cells( 0 )。价值)
下一页
样本输出:
80 2
305 1
23 5
43 3
31 4
80 2
305 1
23 5
如你所见,305获得1.排名,80 - 2.等等on ...
祝你好运!
关于Richard对我的回答的评论 - 小改进(仍使用Dictionary对象):
Dim oDict 作为字典( < span class =code-keyword>整数,元组( 整数, Integer ))= dgv.Rows()。Cast( of DataGridViewRow) _
.GroupBy( Function (r)r.Cells( ColA)。值)_
.OrderByDescending(函数(g)g.Key)_
。选择(功能(grp,index)新 KeyValuePair( 整数,元组( 整数,整数))(grp.Key,Tuple.Create(index + 1,grp。 Count-1)))_
.ToDictionary( Function (kvp)kvp.Key, Function (kvp)kvp.Value)
对于 每个 dgr As DataGridViewRow 在 dgv.Rows
Dim 累积作为 整数 = oDict.TakeWhile(函数(x)x.Key> dgr.Cells( 0 )。值).Sum(函数(x)x.Value.Item2)
dgr.Cells( 1 )。Value = oDict(dgr.Cells(< span class =code-digit> 0 )。值).Item1 +累积
下一步
结果:
80 3
305 1
23 7
43 5
31 6
80 3
305 1
23 7
hello everyone. I am trying to rate a group of numbers in a datagrid containing two columns. I want the cells of the Coulmn B to have values as 1st, 2nd, 3rd etc, based on the values of Column A. For example: Col A Col B 80 2 305 1 23 5 43 4 31 3
What I have tried:
All I know is how to assign the result to the cells of Column B after its found Dim iRow As Integer Dim cellA As Integer Dim cellB As Integer For iRow = 0 To Me.DataGridView1.RowCount - 1 Me.DataGridView1.Rows(iRow).Cells(1).Value = cellA Next iRow = irow + 1
解决方案
Something like this should work, and will correctly rank ties:
Dim cumulativeRank As Integer = 1 For Each group As IGrouping(Of Object, DataGridViewRow) In DataGridView1.Rows.Cast(Of DataGridViewRow)().GroupBy(Function (r) r.Cells(0).Value).OrderByDescending(Function (g) g.Key) Dim rank As Integer = cumulativeRank For Each row As DataGridViewRow In group row.Cells(1).Value = rank cumulativeRank += 1 Next Next
If you don't care about ties, then you don't need the grouping:
Dim rank As Integer = 1 For Each row As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)().OrderByDescending(Function (r) r.Cells(0).Value) row.Cells(1).Value = rank rank += 1 Next
In addition to Richard Deeming[^]'s solution and my comment to His solution...
I'd suggest to use Dictionary[^] object when there are rows with duplicate values inColA
Dim dgv As DataGridView = Me.DataGridView1 Dim oDict As Dictionary(Of Integer, Integer) = dgv.Rows().Cast(Of DataGridViewRow) _ .OrderByDescending(Function(r) r.Cells("ColA").Value) _ .GroupBy(Function(r) r.Cells("ColA").Value) _ .Select(Function(grp, index) New KeyValuePair(Of Integer, Integer)(grp.Key, index+1)) _ .ToDictionary(Function(kvp) kvp.Key, Function(kvp) kvp.Value) For Each dgr As DataGridViewRow In dgv.Rows dgr.Cells(1).Value = oDict(dgr.Cells(0).Value) Next
Sample output:
80 2 305 1 23 5 43 3 31 4 80 2 305 1 23 5
As you can see, 305 gets 1. rank, 80 - 2. and so on...
Good luck!
[EDIT]
As to the Richard's comment to my answer - small improvement (still using Dictionary object):
Dim oDict As Dictionary(Of Integer, Tuple(Of Integer, Integer)) = dgv.Rows().Cast(Of DataGridViewRow) _ .GroupBy(Function(r) r.Cells("ColA").Value) _ .OrderByDescending(Function(g) g.Key) _ .Select(Function(grp, index) New KeyValuePair(Of Integer, Tuple(Of Integer, Integer))(grp.Key, Tuple.Create(index+1, grp.Count-1))) _ .ToDictionary(Function(kvp) kvp.Key, Function(kvp) kvp.Value) For Each dgr As DataGridViewRow In dgv.Rows Dim cumulative As Integer = oDict.TakeWhile(Function(x) x.Key>dgr.Cells(0).Value).Sum(Function(x) x.Value.Item2) dgr.Cells(1).Value = oDict(dgr.Cells(0).Value).Item1 + cumulative Next
Result:
80 3 305 1 23 7 43 5 31 6 80 3 305 1 23 7
这篇关于如何根据数值将数字分为第1,第2等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文