473,403 Members | 2,323 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,403 software developers and data experts.

Sort not working

Hi,

I tried to use the Sort() function of my ArrayList object to sort a list of
the following structures:

Public Structure myData
a as Long
b as Short
End Structure

Therefore, I wrote the following Comparer:

Public Class MyComparer
Implements IComparer

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements System.Collections.IComparer.Compare
Dim one, two myData
one = CType(x, myData)
two = CType(y, myData)

Return Decimal.Compare(one.a, two.a) 'a is the interesting value
End Function
End Class

Now, when I call the sort function like that:

Dim x as new ArrayList
[...] Fill the arraylist
x.Sort(New MyComparer)

The values are sorted, beginning with the lowest value of 'a' at first
position and so on. What I'd really want is the biggest value at first
position...how could I do that?

Thanks,

Peter
Dec 24 '06 #1
1 999
"Peter Schmitz" <Pe**********@discussions.microsoft.coma écrit dans le
message de news: 68**********************************@microsoft.com...

| Therefore, I wrote the following Comparer:

....
|
| Now, when I call the sort function like that:
|
| Dim x as new ArrayList
| [...] Fill the arraylist
| x.Sort(New MyComparer)
|
| The values are sorted, beginning with the lowest value of 'a' at first
| position and so on. What I'd really want is the biggest value at first
| position...how could I do that?

Compare automatically sorts in ascending order; try negating the result of
the comparison :

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements System.Collections.IComparer.Compare
Dim one, two myData
one = CType(x, myData)
two = CType(y, myData)

Return -(Decimal.Compare(one.a, two.a)) 'a is the interesting
value
End Function
End Class

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 24 '06 #2

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

Similar topics

4
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...
40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
4
by: Grahamo | last post by:
Hi I'm working with deployed code and am looking for ways to optimise it and clean up memory leaks etc. I'm working with a template ContainerOfPointers, which is exactly that. It has methods...
7
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...
3
by: Bob Dankert | last post by:
Is there any way to maintain the sort order of a sort on a 2D array? For example: I have the array: 1,a 2,a 3,f 4,a 5,s 6,a 7,z 8,b and sort it by the second column, and I...
7
by: DC Gringo | last post by:
I have a datagrid that won't sort. The event handler is firing and return label text, just not the sort. Here's my Sub Page_Load and Sub DataGrid1_SortCommand: -------------------- Private...
4
by: IGotYourDotNet | last post by:
I'm trying to sort my grid and its working halfway, I can only sort the grid once and it puts everything in ASC order (1234555666) and i can't sort it again to display as DESC order (6665554321), ...
0
by: TM | last post by:
I was able to find the code to list the name and email from the contacts in Outlook 2003, but the sort property is not working. Here is my sample code. For now I am just adding an item to a...
5
by: Neil Chambers | last post by:
Hi All, I'm looking to see if it's feasible to use the SortObjectCommand included in the Microsoft.Powershell.Commands assembly. I have a Dictionary Dictionary<string, intd = new...
13
by: Vbbeginner07 | last post by:
its about Sorting a list view but its not working. please help....... Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader) 'Determine whether the column is the same...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.