首页 > 解决方案 > 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)

标签: vb.net

解决方案


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


推荐阅读