Click here to Skip to main content
15,888,062 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two dimensional integer array x, y as below
{1160, 0}, 
{1560, 400}, 
{11940, 10380}, 
{12480, 540},
{12540, 60},
{12600, 60},
{12720, 120},
{13120, 400},
{13380, 260},
{13680, 300},
{14000, 320}

I need to pick {13120, 400} since x is highest as well as y is highest when comparing with other y elements matching with x highest value. I am doing this in VB.NET. Your help is greatly appreciated. Thanks.

What I have tried:

For intIndex As Integer = 0 To a.GetUpperBound(0)
    If a(intIndex, 0) > intCurrentHighestValue Then
        intCurrentHighestValue = a(intIndex, 0)
        intCurrentHighestValue2 = -1
    End If
    If a(intIndex, 0) = intCurrentHighestValue Then
        If a(intIndex, 1) > intCurrentHighestValue2 Then
            intCurrentHighestValue2 = a(intIndex, 1)

        End If
    End If
Next
Posted
Updated 13-Jun-17 7:59am
v2
Comments
ZurdoDev 13-Jun-17 7:56am    
So, where are you stuck?
Rajaram24101984 13-Jun-17 8:17am    
I did not get the result as expected.. it bring highest value
Richard Deeming 13-Jun-17 9:53am    
In what strange universe is 13120 higher than 14000?!
Nelek 13-Jun-17 10:35am    
First search for MAX on Y, from all possible Y (400 in this case) search for max X (13120 in this case).
First find highest Y, then highest X

Just not so good phrased :)
Richard Deeming 13-Jun-17 10:38am    
In that case, surely you'd find the third item, which has y = 10380, which is clearly higher than 400?

First off, that isn't sorting. Sorting is the process of re-ordering all the elements so they are in a sequence, typically ascending or descending.
What you are doing is finding the maximum value, which is a much simpler thing - you can't just use "Maximum value" code and expect it to sort data, and you can;t really just "tweak" it to do a very different job!
Start reading here: sorting 2d array vb - Google Search[^] - you should get enough ideas to finish your homework yourself.
 
Share this answer
 
Comments
Rajaram24101984 13-Jun-17 10:32am    
When you look at highest x value, it is 14000 but y value is lower. When you look at y value highest is 10380, but x value is lower.. so the answer will be {13120, 400}
Nelek 13-Jun-17 10:36am    
He just expressed himself bad.
Maciej Los 13-Jun-17 13:40pm    
5ed!
I know you want it in VB, but I am not going to give you all ready to go solution...

C++
for (int i = 0; i < number_of_pairs; i++)
{
   if (array_value_y[i] >= value_y_saved)
   {
      value_y_saved = array_value_y[i];

      if (array_value_x[i] >= value_x_saved)
      {
         value_x_saved = array_value_x [i];
      }
   }
}


That should do the trick. Now just adapt it to your needs.

EDIT: As Richard Deeming correctly said... it is not really clear what you want to do, but this should be easy enough to adapt depending on what you need to check first to give the highest priority.
 
Share this answer
 
v3
Comments
Maciej Los 13-Jun-17 13:40pm    
5ed!
Seems, you're talking about Drawing.Point[^]

If i'm right, you can sort them using Linq:
VB.NET
Dim points As List(Of Drawing.Point) = New List(Of Drawing.Point) From 
	{
		New Drawing.Point(1160, 0), 
		New Drawing.Point(1560, 400),  
		New Drawing.Point(11940, 10380),  
		New Drawing.Point(12480, 540), 
		New Drawing.Point(12540, 60), 
		New Drawing.Point(12600, 60), 
		New Drawing.Point(12720, 120), 
		New Drawing.Point(13120, 400), 
		New Drawing.Point(13380, 260), 
		New Drawing.Point(13680, 300), 
		New Drawing.Point(14000, 320)
	}
'case #1
Dim sortedPointsByX = points.OrderByDescending(Function(p) p.X).ToList()

'case #2
Dim sortedPointsByY = points.OrderByDescending(Function(p) p.Y).ToList()


Above code returns:
'#1
14000 320 
13680 300 
13380 260 
13120 400 
12720 120 
12600 60 
12540 60 
12480 540 
11940 10380 
1560 400 
1160 0 

'#2
11940 10380 
12480 540 
1560 400 
13120 400 
14000 320 
13680 300 
13380 260 
12720 120 
12540 60 
12600 60 
1160 0 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900