473,588 Members | 2,574 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help Please - sorting array of objects

Thanks for the help in advance. Does anyone have a code sample for the
following scenario.

how would you sort the array of books (BookView) by author and by price.
if there were a number of items in BookView Array?
Public BookView(0) As Book

Class Book

Public author As String
Public price As Decimal = 0

end class

*** Sent via Developersdex http://www.developersdex.com ***
Feb 1 '07 #1
2 935
I would implement IComparer and write my own sort algorithm. That's the
quickest way to sort on two columns. Does this head you in the right
direction, or do you want some code?

Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
----------------------------------------------
"Larry" <do********@nos pamhere.comwrot e in message
news:eZ******** *****@TK2MSFTNG P05.phx.gbl...
Thanks for the help in advance. Does anyone have a code sample for the
following scenario.

how would you sort the array of books (BookView) by author and by price.
if there were a number of items in BookView Array?
Public BookView(0) As Book

Class Book

Public author As String
Public price As Decimal = 0

end class

*** Sent via Developersdex http://www.developersdex.com ***

Feb 1 '07 #2
You can also let your object (Book) implement IComparable and then
rely on ArrayList.Sort to manage the sorting using the IComparable
implementation you provide.

Public Class Form1
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim books As New System.Collecti ons.ArrayList()
books.Add(New Book("author1", 50))
books.Add(New Book("author2", 14))
books.Add(New Book("author1", 45))
books.Add(New Book("author1", 60))
books.Add(New Book("author2", 3))
books.Add(New Book("author1", 3))
dumpbooks(books )
books.Sort()
dumpbooks(books )
End Sub

Public Sub dumpbooks(ByVal books As System.Collecti ons.ArrayList)
For Each b As Book In books
Console.WriteLi ne(b)
Next
Console.WriteLi ne("====")
End Sub
End Class

Public Class Book
Implements IComparable

Public Sub New(ByVal author As String, ByVal price As Double)
Me.author1 = author
Me.price1 = price
End Sub 'New
Private author1 As String

Public Property Author() As String
Get
Return author1
End Get
Set(ByVal value As String)
author1 = value
End Set
End Property
Private price1 As Double

Public Property Price() As Double
Get
Return price1
End Get
Set(ByVal value As Double)
price1 = value
End Set
End Property

Public Overrides Function ToString() As String
Return author1 + " : " + price1.ToString ()
End Function 'ToString
Public Function CompareTo(ByVal obj As Object) As Integer
Implements System.ICompara ble.CompareTo
If obj Is Nothing Then
Return 1
Else
Dim b As Book = CType(obj, Book)
Dim c As Integer = Me.Author.Compa reTo(b.Author)
If c = 0 Then
Return Me.Price.Compar eTo(b.Price)
Else
Return c
End If
End If

End Function
End Class

===============
Clay Burch
Syncfusion, Inc.

Feb 1 '07 #3

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

Similar topics

0
1309
by: Phil Powell | last post by:
I am having to sort an array of objects by an arbitrary object field name that you are not going to know in advance, therefore, I cannot name the sorting function accordingly nor reference the object accordingly: class ResizeView extends View { function displayHTML() { $field = $safe->getField();
2
3061
by: D. Roshani | last post by:
Hello ! I wonder if any one can help me to create a cosomize sorting order (as Macro or added small program in c++ or c# which does this work) in a Access Database contaning one table only words encoded in ISO-8859-1 A a B b C c D d E e É é F f G g H h I i Í í J j 1 2 3 4 5 6 7 8 9 10 11 12 Jh jh K k L l ll M m N n O o P p Q q R r rr S s...
7
3253
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)
0
2653
by: Alex Vinokur | last post by:
=================================== ------------- Sorting ------------- Comparative performance measurement =================================== Testsuite : Comparing Function Objects to Function Pointers Source : Technical Report on C++ Performance Tool : The C/C++ Program Perfometer (Version 2.8.1-1.19-Beta) * http://sourceforge.net/projects/cpp-perfometer/
5
8969
by: Dr. Ann Huxtable | last post by:
Hello All, I am reading a CSV (comma seperated value) file into a 2D array. I want to be able to sort multiple columns (ala Excel), so I know for starters, I cant be using the array, I need something more sophistictated like a vector of row objects. The data is of this format (same number of columns for each row): A, x, y, z, ...
22
4132
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b) { return -1; } else
1
2627
by: aredo3604gif | last post by:
On Sun, 10 Apr 2005 19:46:32 GMT, aredo3604gif@yahoo.com wrote: >The user can dynamically enter and change the rule connection between >objects. The rule is a "<" and so given two objects: >a < b simply means that b < a can't be set, also it must be a != b. >And with three objects a < b , b < c means a < c > >I studied Quick Union Find algorithms a bit and if I understood them >correctly, once the user gives the input setting the...
5
2517
by: DKC | last post by:
Hi, Using VB.NET. I have a datagrid having a strongly typed array of objects as its data source. The data from the array of objects is displayed by means of a table style, which is fine, but I cannot sort the data when I click on the column header. I have set the tablestype.allowsorting = true, but this has no effect.
2
2173
by: RC | last post by:
http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_sort2 You can see above link or read below i copy/paste from above link <script type="text/javascript"> function sortNumber(a, b) { return a - b }
2
1191
by: PillarOfCreation | last post by:
hello everybody.. im new here and new to java :) please help with your expertise i need to sort an array of string objects..by lexicographical order. by method compareTo() only. cant use array.sort() and such. if i have several words in the array. and i need to enter a new word that is lexicographicaly lower from one of the words, how do i make room for it ,move the rest, and put it in the right spot.. i tried bubblesort and it didn\t...
0
7927
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
7857
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8352
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
7981
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,...
1
5723
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
5396
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2367
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
1
1457
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.