473,792 Members | 2,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

understanding Array.sort

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(Inpu tArray, New FooComparer)
...
...
...
Public Class FooComparer
Implements IComparer

Public Function Compare( _
ByVal x As Object, _
ByVal y As Object _
) As Integer Implements IComparer.Compa re
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

Nov 21 '05 #1
5 1434
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.ne t> wrote in message
news:AA******** *************** ***********@mic rosoft.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(Inpu tArray, New FooComparer)
..
..
..
Public Class FooComparer
Implements IComparer

Public Function Compare( _
ByVal x As Object, _
ByVal y As Object _
) As Integer Implements IComparer.Compa re
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

Nov 21 '05 #2

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.WriteLi ne 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.
Nov 21 '05 #3
Irfan,

"Irfan Mumtaz" <sp****@spam.ne t> 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.or g/wiki/Sorting_algorit hm>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4
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.ne t> wrote in message
news:AA******** *************** ***********@mic rosoft.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(Inpu tArray, New FooComparer)
..
..
..
Public Class FooComparer
Implements IComparer

Public Function Compare( _
ByVal x As Object, _
ByVal y As Object _
) As Integer Implements IComparer.Compa re
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


Nov 21 '05 #5
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.ne t> wrote in message
news:F5******** *************** ***********@mic rosoft.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.ne t> wrote in message
news:AA******** *************** ***********@mic rosoft.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(Inpu tArray, New FooComparer)
..
..
..
Public Class FooComparer
Implements IComparer

Public Function Compare( _
ByVal x As Object, _
ByVal y As Object _
) As Integer Implements IComparer.Compa re
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


Nov 21 '05 #6

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

Similar topics

9
17608
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 array_reverse(). Can anyone tell me why?
4
3768
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...
7
3267
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)) CType(local4, Short) = CType(src, Short)
7
25174
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 (Bubble, insertion, selection..). I have an array of int's and am passing them to each sort function.
3
2476
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 am trying to calculate, with each sort, how many times during the sort the array elements are compared and swapped.
5
2219
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 1,1,2,4. I have narrowed it down to the sort function. I'm almost positive
26
2209
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 total calories that the user input together. determine which food has the largest calories. how do i start to modifiy the program inorder to do the things i listed above. thanks #include <stdio.h> #include <stdlib.h>
5
2841
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 values 28 142 3 17 225. I can sort this array in ascending order and it will return 3 17 28 142 225. But I want a method that will return the sort order, not the array in sorted
24
3462
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 declared an array as: char *stringArray = {"one","two","three","a"}; When I pass the array using:
0
9670
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10430
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10211
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10159
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2917
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.