hi,
I had posted a question in the group "how to arrange arrays in increasing
order" Although i got an answer using IComparable Interface by Harfried
,which is given below.
Dim InputArray()() As Integer = _
New Integer()() { _
New Integer() {2, 0, 0}, _
New Integer() {1, 0, 0}, _
New Integer() {6, 0, 0}, _
New Integer() {3, 0, 0} _
}
Array.Sort(InputArray, New FooComparer)
...
...
...
Public Class FooComparer
Implements IComparer
Public Function Compare( _
ByVal x As Object, _
ByVal y As Object _
) As Integer Implements IComparer.Compare
Dim xx As Integer() = DirectCast(x, Integer())
Dim yy As Integer() = DirectCast(x, Integer())
Return yy(0) - xx(0)
End Function
End Class
/////
However, i fail to understand how 'compare' method works. In particular,
when we are comparing only two element at a time how does it sort all the
elements in order ? Am i missing something in understanding array. Can
someone give me some ideas as i need it to apply to some other classes.
TIA 5 1410
Any computer sorting algorithm consists of three main parts:
the picker, which decides which two items to compare next
the comparer, which decides which one of two items should precede the
other in the output array,
the swapper, which uses what the comparer told it to adjust the
locations of the two items
When you talk about bubble-sorts, quicksorts, and so on, you're really
talking about the picker part of the sorter. Most of the speed of a sort
algorithm comes from the algorithm the picker uses, but sometimes the
swapper can do useful things, like leaving the actual items in place but
swapping references to them.
So your implementation of IComparer is supplying just the comparer part of
the algorithm. Each time it's called, all you are concerned with is telling
the sorter which one of the two should come before the other.
HTH,
Tom Dacon
Dacon Software Consulting
"Irfan Mumtaz" <sp****@spam.net> wrote in message
news:AA**********************************@microsof t.com... hi, I had posted a question in the group "how to arrange arrays in increasing order" Although i got an answer using IComparable Interface by Harfried ,which is given below. Dim InputArray()() As Integer = _ New Integer()() { _ New Integer() {2, 0, 0}, _ New Integer() {1, 0, 0}, _ New Integer() {6, 0, 0}, _ New Integer() {3, 0, 0} _ } Array.Sort(InputArray, New FooComparer) .. .. .. Public Class FooComparer Implements IComparer
Public Function Compare( _ ByVal x As Object, _ ByVal y As Object _ ) As Integer Implements IComparer.Compare Dim xx As Integer() = DirectCast(x, Integer()) Dim yy As Integer() = DirectCast(x, Integer()) Return yy(0) - xx(0) End Function End Class /////
However, i fail to understand how 'compare' method works. In particular, when we are comparing only two element at a time how does it sort all the elements in order ? Am i missing something in understanding array. Can someone give me some ideas as i need it to apply to some other classes.
TIA
The framework internally uses a quicksort algorithim to loop through
the data and call your Compare method as needed to sort the items.
You don't need to bother with looping or comparing multiple
items--it's handled internally by the framework.
For a better explanation, add a Console.WriteLine call inside your
compare method and then call Array.Sort. You'll see your method gets
called many times.
HTH,
Sam However, i fail to understand how 'compare' method works. In particular, when we are comparing only two element at a time how does it sort all the elements in order ? Am i missing something in understanding array. Can someone give me some ideas as i need it to apply to some other classes.
TIA
B-Line is now hiring one VB.NET developer for
WinForms + WebServices position with ASPX in future.
Seaking mid to senior level developer. For
information or to apply e-mail sam_blinex_com.
Irfan,
"Irfan Mumtaz" <sp****@spam.net> schrieb: However, i fail to understand how 'compare' method works. In particular, when we are comparing only two element at a time how does it sort all the elements in order ? Am i missing something in understanding array. Can someone give me some ideas as i need it to apply to some other classes.
Sorting algorithm
<URL:http://en.wikipedia.org/wiki/Sorting_algorithm>
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
thanks for the reply,
just to close the loop...
Since the compare method returns 1, 0 or -1 when two elements are compared,
does it mean when the result is greater than 1, the element being compared
moves up and when it is less then one, it moves down ?
irfan
"Tom Dacon" wrote: Any computer sorting algorithm consists of three main parts:
the picker, which decides which two items to compare next the comparer, which decides which one of two items should precede the other in the output array, the swapper, which uses what the comparer told it to adjust the locations of the two items
When you talk about bubble-sorts, quicksorts, and so on, you're really talking about the picker part of the sorter. Most of the speed of a sort algorithm comes from the algorithm the picker uses, but sometimes the swapper can do useful things, like leaving the actual items in place but swapping references to them.
So your implementation of IComparer is supplying just the comparer part of the algorithm. Each time it's called, all you are concerned with is telling the sorter which one of the two should come before the other.
HTH, Tom Dacon Dacon Software Consulting
"Irfan Mumtaz" <sp****@spam.net> wrote in message news:AA**********************************@microsof t.com... hi, I had posted a question in the group "how to arrange arrays in increasing order" Although i got an answer using IComparable Interface by Harfried ,which is given below. Dim InputArray()() As Integer = _ New Integer()() { _ New Integer() {2, 0, 0}, _ New Integer() {1, 0, 0}, _ New Integer() {6, 0, 0}, _ New Integer() {3, 0, 0} _ } Array.Sort(InputArray, New FooComparer) .. .. .. Public Class FooComparer Implements IComparer
Public Function Compare( _ ByVal x As Object, _ ByVal y As Object _ ) As Integer Implements IComparer.Compare Dim xx As Integer() = DirectCast(x, Integer()) Dim yy As Integer() = DirectCast(x, Integer()) Return yy(0) - xx(0) End Function End Class /////
However, i fail to understand how 'compare' method works. In particular, when we are comparing only two element at a time how does it sort all the elements in order ? Am i missing something in understanding array. Can someone give me some ideas as i need it to apply to some other classes.
TIA
Yeah, basically that's it.
If you implement your own IComparer, its Compare method gets objects X and
Y. If you return a negative number, that says that X should precede Y in the
output; if you return zero, that says they're equal; if you return a
positive number, that says that X should follow Y in the output.
"Irfan Mumtaz" <sp****@spam.net> wrote in message
news:F5**********************************@microsof t.com... thanks for the reply, just to close the loop... Since the compare method returns 1, 0 or -1 when two elements are
compared, does it mean when the result is greater than 1, the element being
compared moves up and when it is less then one, it moves down ?
irfan
"Tom Dacon" wrote:
Any computer sorting algorithm consists of three main parts:
the picker, which decides which two items to compare next the comparer, which decides which one of two items should precede
the other in the output array, the swapper, which uses what the comparer told it to adjust the locations of the two items
When you talk about bubble-sorts, quicksorts, and so on, you're really talking about the picker part of the sorter. Most of the speed of a sort algorithm comes from the algorithm the picker uses, but sometimes the swapper can do useful things, like leaving the actual items in place but swapping references to them.
So your implementation of IComparer is supplying just the comparer part
of the algorithm. Each time it's called, all you are concerned with is
telling the sorter which one of the two should come before the other.
HTH, Tom Dacon Dacon Software Consulting
"Irfan Mumtaz" <sp****@spam.net> wrote in message news:AA**********************************@microsof t.com... hi, I had posted a question in the group "how to arrange arrays in
increasing order" Although i got an answer using IComparable Interface by
Harfried ,which is given below. Dim InputArray()() As Integer = _ New Integer()() { _ New Integer() {2, 0, 0}, _ New Integer() {1, 0, 0}, _ New Integer() {6, 0, 0}, _ New Integer() {3, 0, 0} _ } Array.Sort(InputArray, New FooComparer) .. .. .. Public Class FooComparer Implements IComparer
Public Function Compare( _ ByVal x As Object, _ ByVal y As Object _ ) As Integer Implements IComparer.Compare Dim xx As Integer() = DirectCast(x, Integer()) Dim yy As Integer() = DirectCast(x, Integer()) Return yy(0) - xx(0) End Function End Class /////
However, i fail to understand how 'compare' method works. In
particular, when we are comparing only two element at a time how does it sort all
the elements in order ? Am i missing something in understanding array. Can someone give me some ideas as i need it to apply to some other
classes. TIA This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: lawrence |
last post by:
Is there an easy way to sort a 2 dimensional array alphabetically by
the second field in each row?
Also, when I use sort() on a two dimensional array, it seems to work a
lot like...
|
by: its me |
last post by:
Let's say I have a class of people...
Public Class People
Public Sex as String
Public Age as int
Public Name as string
end class
And I declare an array of this class...
|
by: Federico G. Babelis |
last post by:
Hi All:
I have this line of code, but the syntax check in VB.NET 2003 and also in
VB.NET 2005 Beta 2 shows as unknown:
Dim local4 As Byte
Fixed(local4 = AddressOf dest(offset))
...
|
by: ritchie |
last post by:
Hi all,
I am new to this group and I have question that you may be able to
help me with.
I am trying to learn C but am currently stuck on this.
First of all, I have a function for each sort...
|
by: ritchie |
last post by:
Hi all!
Still working on this program!
Just to recap, I am writing a program to sort an array with four
different sort algorythms.
I am having a little trouble at the moment though!
Now, I...
|
by: ritchie |
last post by:
Hi,
I am writing to ask if anyone can see why my array is not being sorted
correctly?
It's an array of 4 elements(ints 1,2,3,4) but after calling the
selection sort
it comes back sorted as...
|
by: Bail |
last post by:
I will have a exam on the oncoming friday, my professor told us that it
will base upon this program. i am having troubles understanding this
program, for example what if i want to add all the...
|
by: Jan Smith |
last post by:
I've searched the overloads for the Array.Sort method, and I haven't found a
clear answer to my question. Maybe it's not in Array.Sort.
Here's the question:
I initialize an array X with the...
|
by: Michael |
last post by:
Hi,
I am trying to pass a function an array of strings, but I am having trouble
getting the indexing to index the strings rather than the individual
characters of one of the strings.
I have...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
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)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
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
|
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...
| |