Arrays VBNET中的数组排序
我有二维整数Arrays VBNET中的数组排序,arrays,vb.net,sorting,Arrays,Vb.net,Sorting,我有二维整数数组x,y如下 {1160, 0}, {1560, 400}, {11940, 10380}, {12480, 540}, {12540, 60}, {12600, 60}, {12720, 120}, {13120, 400}, {13380, 260}, {13680, 300}, {14000, 320} 我需要选择{13120400}。我通过按降序遍历x值,并在y值比上一条记录增加的地方取第一对来实现这一点 试试这个: Function GetLargestPair
数组x,y
如下
{1160, 0},
{1560, 400},
{11940, 10380},
{12480, 540},
{12540, 60},
{12600, 60},
{12720, 120},
{13120, 400},
{13380, 260},
{13680, 300},
{14000, 320}
我需要选择{13120400}
。我通过按降序遍历x
值,并在y
值比上一条记录增加的地方取第一对来实现这一点 试试这个:
Function GetLargestPair(ByVal data(,) As Integer) As Integer()
'First convert to data structure that's easier to use
Dim records As New List(Of Integer())
For i As Integer = 0 To data.GetUpperBound(0) - 1
records.Add(New Integer() {data(i, 0), data(i, 1)})
Next i
'Now it's very easy to sort descending by the X values:
Dim sorted = records.OrderByDescending(Function(r) r(0)).ThenBy(Function(r) r(1))
'And then use that to find the result
Dim PrevY As Integer = Integer.MaxValue
For Each pair As Integer() In sorted
If pair(1) > PrevY Then Return pair
PrevY = pair(1)
Next
'Not clear what you want in this case. I just returned the overall largest X,
' but you might prefer Nothing or throwing an exception
Return sorted.First()
End Function
可以这样称呼:
'Sample data
Dim data = {{1160, 0}, {1560, 400}, {11940, 10380}, {12480, 540}, {12540, 60}, {12600, 60}, {12720, 120}, {13120, 400}, {13380, 260}, {13680, 300}, {14000, 320}}
Dim result = GetLargestPair(data)
'result should now contain {13120, 400}
到目前为止你试过什么,代码方面的?你是说{13120400}比{1194010380}强吗因为有两个项目的y=400,只有一个项目的y=10380?我同意@AndrewMorton的观点,我有点困惑为什么这个项目是被选中的-你能解释一下你是如何得出这个结论的吗?因为与其他项目的y值相比,x值是最高的。。所以我来决定为什么{13120400}击败{1400320}?你需要能够写清楚的规则来描述如何订购物品。你是对的。。。您可以对这些数据进行抽样,您可以对这些数据进行抽样,这数据是一个数据的di数据。您可以对这些数据进行抽样,这数据是一个数据的di数据的di数据,您可以对这些数据进行样本,这数据是一个数据的Dim数据的数据,这些数据的di数据是:{{{{{22380,8380,8380,8380,8380,8380,8380,8380,8380,8380,8380,8380,8380,8380,8380,8380,8380,8380,8380,8380,80,80,80,30,160,160,,,,,,{22640,160,160,160,160,160,160,160,160,160,160,160,160,160,100,,,,,,,,,,,,,,,,,,,,{227020 20 20 20 20 20 20 20 20 20 20 20,40,40,40,40,100,,,,,,,,{25060,120},{25220,160},{25340,120},{25420,80},{25500,80},{25500,0},{25460,-40},{25420,-40},{25320,-100},{25120,-200},{25020,-100}答案将是{25500,80}哦,是的。{25420,80}结果是{25420,80},其中为{25500,80}是的,我发现另一个样本数据Dim data={40,0},{140,100},{260,120},{460,200},{1000,540},{1360,360},{11980,10620},{12360,380},{13020,660},{13260,240},{13460,200},{13580,120},{13720,{13780,60},{13820,40},{13860,40},{14800,940},{14800,0},{14860,60},{15040,180},{15180,140},{15340,160}答案是{14800,940},{14800,0},{14820},{14860,40},{15040,180},{15340,160}但它给出了{15040180},因为根据以前的规则,940大于18014800不再是“最大的X”群的一部分。如果这很重要,因为Y要大得多,那么Y就很重要了(1198010620)应该更重要。