vb.net - Show First 10 Highest/Lower value in my Line Textbox
问题描述
I'd like to deepen this code, and show me the top 10 highest values, and the 10 lowest values. do you think it would be possible following the example given to me?
Dim myList = TxtNumberListCount.Lines.ToList
Dim removedEmptyLinesCount = myList.RemoveAll(Function(str) String.IsNullOrWhiteSpace(str))
Dim minValue = myList.Select(Function(line)
Dim res = 0
Integer.TryParse(line, res)
Return res
End Function).Min() ' or Max()
Dim lineIndex = myList.IndexOf(minValue) + removedEmptyLinesCount
TxtBoxMinValue1.Text = minValue
TxtBoxCountMinValue1.Text = lineIndex
Dim myList1 = TxtNumberListCount.Lines.ToList
Dim removedEmptyLinesCount1 = myList1.RemoveAll(Function(str) String.IsNullOrWhiteSpace(str))
Dim maxValue = myList1.Select(Function(line)
Dim res = 0
Integer.TryParse(line, res)
Return res
End Function).Max() ' or Max()
Dim lineIndex1 = myList1.IndexOf(maxValue) + removedEmptyLinesCount
TxtBoxMaxValue1.Text = maxValue
TxtBoxCountMaxValue1.Text = lineIndex1
TextBox6.Text = TxtNumberListScan.Lines(TxtBoxCountMinValue1.Text)
TextBox7.Text = TxtNumberListScan.Lines(TxtBoxCountMaxValue1.Text)
解决方案
To get the most 10 highest values:
Dim Top10HighestValues = myList.Select(Function(line)
Dim res = 0
Integer.TryParse(line, res)
Return res
End Function).OrderByDescending(Function(x) x).Take(10)
And to get the most 10 lowest values you need to replace OrderByDescending
with OrderBy
:
Dim Top10LowestValues = myList.Select(Function(line)
Dim res = 0
Integer.TryParse(line, res)
Return res
End Function).OrderBy(Function(x) x).Take(10)
Save values and indexes into a Dictionary:
Dim dictionary As New Dictionary(Of String, Integer)
For Each i In Top10HighestValues
Dim idx = myList.IndexOf(i) + removedEmptyLinesCount
dictionary.Add(i, idx)
Next
For Each d In Dictionary
TextBox2.AppendText(d.Key & vbCrLf)
TextBox2.AppendText(d.Value & vbCrLf)
Next
Edit to get the indexes
推荐阅读
- java - Tomcat 拒绝所有 POST 请求
- python - 如何在 python 中解决一组 ODE,其中一个包含 dirac delta 函数(或逐步增量)?
- php - Zf3:每个模块中的类似Controllers
- c# - C#从Rabbit MQ每x间隔以y批量大小消费/读取消息
- javascript - 异步 Javascript:意外结果
- git - Azure Build Pipeline 从 git repo 获取最新标签
- python - 如何使用 python 创建 EC2 实例并附加已创建的安全组
- javascript - 将一个页面重定向到另一个页面 10 秒,然后再次重定向回原始 URL
- swift - 为什么这段代码会产生如此令人困惑的错误信息?
- r - 用 dopar 迭代地运行 foreach