473,320 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Sorting Points

Is there any easy way to sort a list of Points by either its X or Y or
both?

Tried
sortedlist (of String, Point) where string is Point.X & "," & Point.Y
as the key
sortedlist (of Point, Point) using the Point as the key

I could always use try and true method of a binary search algorithm
but I rather not.

It seems to be storing it but I can't seem to extract the data

Feb 26 '07 #1
4 4602
On Feb 26, 7:13 am, short...@gmail.com wrote:
Is there any easy way to sort a list of Points by either its X or Y or
both?

Tried
sortedlist (of String, Point) where string is Point.X & "," & Point.Y
as the key
sortedlist (of Point, Point) using the Point as the key

I could always use try and true method of a binary search algorithm
but I rather not.

It seems to be storing it but I can't seem to extract the data
You could write an IComparer that knows how to compare Points by their
coordinates and pass that in to the Sort method of a list.
Feb 26 '07 #2


"Chris Dunaway" <du******@gmail.comwrote in message
news:11********************@m58g2000cwm.googlegrou ps.com...
On Feb 26, 7:13 am, short...@gmail.com wrote:
>Is there any easy way to sort a list of Points by either its X or Y or
both?

Tried
sortedlist (of String, Point) where string is Point.X & "," & Point.Y
as the key
sortedlist (of Point, Point) using the Point as the key

I could always use try and true method of a binary search algorithm
but I rather not.

It seems to be storing it but I can't seem to extract the data

You could write an IComparer that knows how to compare Points by their
coordinates and pass that in to the Sort method of a list.

EG
Function CompareX(ByVal a As Point, ByVal b As Point) As Integer
Return a.X.CompareTo(b.X)
End Function

Function CompareY(ByVal a As Point, ByVal b As Point) As Integer
Return a.Y.CompareTo(b.Y)
End Function

Sub Main()

Dim points() As Point = {New Point(3, 4), New Point(1, 5), New Point(7,
2)}
Array.Sort(Of Point)(points, AddressOf CompareX)
Console.WriteLine("Array of points sorted by X")
For Each p As Point In points
Console.WriteLine(String.Format("x={0},y={1}", p.X, p.Y))
Next

Dim pointList As New List(Of Point)(points)
pointList.Sort(AddressOf CompareY)

Console.WriteLine("List of points sorted by Y")
For Each p As Point In pointList
Console.WriteLine(String.Format("x={0},y={1}", p.X, p.Y))
Next

Console.ReadKey()

End Sub

Davud

Feb 26 '07 #3
On Feb 26, 10:05 am, "David Browne" <davidbaxterbrowne no potted
m...@hotmail.comwrote:
>
Dim points() As Point = {New Point(3, 4), New Point(1, 5), New Point(7,
2)}
Array.Sort(Of Point)(points, AddressOf CompareX)
How does this work? I cant find a reference in the docs to an
Array.Sort method that takes that kind of delegate? I tested the code
and it works, of course, I am just at a loss to understand how?

Thanks,

Chris

Feb 26 '07 #4


"Chris Dunaway" <du******@gmail.comwrote in message
news:11*********************@m58g2000cwm.googlegro ups.com...
On Feb 26, 10:05 am, "David Browne" <davidbaxterbrowne no potted
m...@hotmail.comwrote:
>>
Dim points() As Point = {New Point(3, 4), New Point(1, 5), New
Point(7,
2)}
Array.Sort(Of Point)(points, AddressOf CompareX)

How does this work? I cant find a reference in the docs to an
Array.Sort method that takes that kind of delegate? I tested the code
and it works, of course, I am just at a loss to understand how?
That's this overload

Public Shared Sub Sort(Of T) ( _
array As T(), _
comparison As Comparison(Of T) _
)
http://msdn2.microsoft.com/en-us/library/cxt053xf.aspx

Which uses the Comparison generic delegate

Public Delegate Function Comparison(Of T) ( _
x As T, _
y As T _
) As Integer
http://msdn2.microsoft.com/en-us/library/tfakywbh.aspx

David

Feb 26 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Oldchatterman | last post by:
Hello, in an application I measure a lot of 2d coordinates (x,y) of a pattern. This pattern consists of a set of points on grid with fixed pitches in x and y direction. These coordinates all...
18
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite...
5
by: MikeY | last post by:
Hi Eveyone, I'm trying to sort my ArrayList, but I'm getting stumped. Hopefully someone can help me out. I'm using C# Windows forms, but Console app for testing. What I am doing is pulling my...
5
by: JustSomeGuy | last post by:
According to the docs... http://www.cppreference.com/cpplist/sort.html There is a parameter to the std::list.sort method. I don't understand this methods use... I want to sort the list which...
3
by: acosgaya | last post by:
Hi, I would like some help as how to approach the following problem: I have a set of d-dimensional points (e.g (3,5,8,9,5) is a 5-dimensional point), and I need to sort the points on each of the...
1
by: Jeff | last post by:
ok gang. I have this code to get the data from mssql db set standings1 = conn.execute("select points, team, wins, losses, win_pct from tblStats order by wins desc, points desc, team asc")...
11
by: garyhoran | last post by:
Hi Guys, I have a collection that contains various attributes (stuff like strings, DateTime and Timespan) . I would like to access the collection in various orders at different points in the...
5
by: saytri | last post by:
I've got a problem in sorting. I have to use a bubble sort to sort the scores with those who bring the highest percent at the top. The scores of each user i saved in a text file. This is how i...
4
by: gamernaveen | last post by:
Hello everyone ! , I am working on a rating field in my database. Basically , my rating formula goes like this RATING = TOTAL POINTS / TOTAL RATINGS Ofcourse , its the average rating. I store...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.