473,385 Members | 1,712 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,385 software developers and data experts.

a sorted by field1 then by field2 collection, speed critical, any advice?

Hello,
I have an application that requires a collection that is sorted by a double
field first, and then by a long field.

So, lets say I have an class that has 2 fields called time, priority. I
want to put a bunch of those classes into a collection and have that
collection always stay sorted by time first, then priority. The last object
in the list should be the lowest time, highest priority object, etc, for
easy picking.

Object Time Priority
4 105 56
5 105 98
6 105 100
1 100 98
2 100 99
3 100 100

Anyone have any advice on how they would design this? My current
implementation uses a single arraylist and when i insert an object, I divide
and conquer to find where I should insert. I need to find a faster solution
and was hoping someone has done some research into this already in the past.

I thank you in advance,
Dan

Nov 15 '05 #1
1 1748
Hi Dan,

You have two separate issues here. The first is comparing objects by two
fields. The second is maintaining a list of objects in sorted order.

The first one is simple, the second gets a little more complicated. To
compare objects first by time, then by priority, you would implement
IComparable for your class. However in your case time is a double. It
becomes very likely then, that the times of your objects will be distinct,
and there will be no need to compare by priority. But if that happens in
your scenario, then you would do it by implementing IComparable, your
compare function might look something like this:

public int CompareTo(object obj)
{
TimePriority timePriority = obj as TimePriority;
if (timePriority != null)
{
if (this.time != timePriority.time)
{
return this.time < timePriority.time ? 1 : -1;
}
if (this.priority != timePriority.priority)
{
return this.priority < timePriority.priority ? -1 : 1;
}
}
return 0;
}

The second issue of maintaining a sorted array of objects really depends on
what you are doing with the objects. If it is a list in which you will
insert the members only once, and then take them off the back as you imply,
you can just add them in any order to the ArrayList and then call .Sort().
The sort function will use your IComaparable implementation. Remove from
the back of the ArrayList is constant time.

If you will be adding and removing these events over time, then its likely
that you want to use a balanced binary tree or a Heap. It might be more
trouble than its worth for you to implement either of these yourself, as it
can be kind of tricky to do performantly, but you will likely be able to
find a good library somewhere online. The balanced binary tree will offer
performance and allow you to maintain a collection in sorted order while
you insert and remove elements. If you only ever need to know the lowest
time/highest priority object though, you can get by with a Heap, which
doesn't keep the elements in sorted order, but can tell you the 'min'
quickly. This will be much better than your ArrayList insert.

Do a google search on 'Balanced Binary Tree' or 'Red-Black Tree'

HTH
-DoRon
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.

--------------------
From: "Dan H." <Da**@projectavatar.com>
Subject: a sorted by field1 then by field2 collection, speed critical, any advice?Date: Fri, 26 Sep 2003 18:27:03 -0400
Lines: 28
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <OA**************@TK2MSFTNGP09.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: pcp01605659pcs.vnburn01.mi.comcast.net 68.42.241.171
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP09.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:187621
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

Hello,
I have an application that requires a collection that is sorted by a double
field first, and then by a long field.

So, lets say I have an class that has 2 fields called time, priority. I
want to put a bunch of those classes into a collection and have that
collection always stay sorted by time first, then priority. The last objectin the list should be the lowest time, highest priority object, etc, for
easy picking.

Object Time Priority
4 105 56
5 105 98
6 105 100
1 100 98
2 100 99
3 100 100

Anyone have any advice on how they would design this? My current
implementation uses a single arraylist and when i insert an object, I divideand conquer to find where I should insert. I need to find a faster solutionand was hoping someone has done some research into this already in the past.
I thank you in advance,
Dan


Nov 15 '05 #2

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

Similar topics

5
by: perryche | last post by:
Problem #1: I have a form, which says, has 2 fields: F1 & F2 (both Dates). I want to be able to change F1 and F2 will change to be the same as F1 in the codes. However, I will still be able...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
16
by: Ben Hannon | last post by:
Hi, I'm writting a COM Class in VB.NET to be used in a VB6 project (Tired of the VB6 hassles with cloning and serializing an object). All my classes I need cloneable/serializable are now in a...
4
by: Dave | last post by:
Hi. I am learning PyOpenGL and I am working with a largish fixed scene composed of several thousand GLtriangles. I plan to store the coords and normals in a NumPy array. Is this the fastest...
9
by: burningsunorama | last post by:
Hi guys! This is maybe a too 'academic problem', but I would like to hear your opinions, something like pros and cons for each approach.... ... Recently we've had at work a little talk about the...
9
by: bonk | last post by:
Does anyone have a simple example on how to prohibit that any thread other than the current thread modifies a certain object (a collection) while we are in a certain section of the code? In other...
5
by: WebSnozz | last post by:
Some collections are such that efficient search algorithms work on them such as binary search if the collection is a type which is sorted. I'm wondering how LINQ searches these collections and if...
18
by: Hunk | last post by:
Would like some advice on the fillowing I have a sorted list of items on which i require to search and retrieve the said item and also modify an item based on its identity. I think an Map stl...
8
by: Guy | last post by:
Is there a better way to search identical elements in a sorted array list than the following: iIndex = Array.BinarySearch( m_Array, 0, m_Array.Count, aSearchedObject ); aFoundObject= m_Array;...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.