在“行”文本框中显示前10个最高/最低值

时间:2019-04-09 16:25:01

标签: vb.net

我想加深这段代码,并向我展示前10个最高值和10个最低值。您认为有可能按照我给的例子吗?

  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)

1 个答案:

答案 0 :(得分:1)

要获取最多的10个最高值:

Dim Top10HighestValues = myList.Select(Function(line)
                                          Dim res = 0
                                          Integer.TryParse(line, res)
                                          Return res
                                        End Function).OrderByDescending(Function(x) x).Take(10)

要获得最多10个最低值,您需要将OrderByDescending替换为OrderBy

Dim Top10LowestValues = myList.Select(Function(line)
                                          Dim res = 0
                                          Integer.TryParse(line, res)
                                          Return res
                                        End Function).OrderBy(Function(x) x).Take(10)

将值和索引保存到字典中:

  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

编辑以获取索引