473,837 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorting and non-unique keys

Hi Guys,

I have a collection that contains various attributes (stuff like
strings, DateTime and Timespan) . I would like to access the collection
in various orders at different points in the application ie sometimes I
want to cycle through the values in the collection in DateTime order
while at other times in TimeSpan order.

Ideally I would like multiple keys - such as Timespan within DateTime
order but maybe that is asking too much.

Also some of the keys I would like to use are not unique.

So I guess I have three questions

1. Whats the best way of sorting a collection in general
2. Is there anyway to sort with multiple keys
3. Is there any way to do sorts with non-unique keys

Would really appreciate any info you guys could give me.

Regards,

Gary

Nov 12 '06 #1
11 7810
Would it be an idea not to have just one data collection,
but to have, in addition, several key collections that you
can sort and that give access to your data collection?

Adrian.

<ga*******@gmai l.comwrote in message
news:11******** **************@ h54g2000cwb.goo glegroups.com.. .
Hi Guys,

I have a collection that contains various attributes (stuff like
strings, DateTime and Timespan) . I would like to access the collection
in various orders at different points in the application ie sometimes I
want to cycle through the values in the collection in DateTime order
while at other times in TimeSpan order.

Ideally I would like multiple keys - such as Timespan within DateTime
order but maybe that is asking too much.

Also some of the keys I would like to use are not unique.

So I guess I have three questions

1. Whats the best way of sorting a collection in general
2. Is there anyway to sort with multiple keys
3. Is there any way to do sorts with non-unique keys

Would really appreciate any info you guys could give me.

Regards,

Gary

Nov 12 '06 #2
Gary,

You can use a sorted list.

If you have more keys, you can create more in a way that there is one that
holds the data and others which holds the references to that one in the key
value pair.

Cor

<ga*******@gmai l.comschreef in bericht
news:11******** **************@ h54g2000cwb.goo glegroups.com.. .
Hi Guys,

I have a collection that contains various attributes (stuff like
strings, DateTime and Timespan) . I would like to access the collection
in various orders at different points in the application ie sometimes I
want to cycle through the values in the collection in DateTime order
while at other times in TimeSpan order.

Ideally I would like multiple keys - such as Timespan within DateTime
order but maybe that is asking too much.

Also some of the keys I would like to use are not unique.

So I guess I have three questions

1. Whats the best way of sorting a collection in general
2. Is there anyway to sort with multiple keys
3. Is there any way to do sorts with non-unique keys

Would really appreciate any info you guys could give me.

Regards,

Gary

Nov 12 '06 #3
But a sorted list requires a unique key right?

Any options for non unique keys?

Gary

On Nov 12, 10:31 pm, "Cor Ligthert [MVP]" <notmyfirstn... @planet.nl>
wrote:
Gary,

You can use a sorted list.

If you have more keys, you can create more in a way that there is one that
holds the data and others which holds the references to that one in the key
value pair.

Cor

<garyho...@gmai l.comschreef in berichtnews:11* *************** ******@h54g2000 cwb.googlegroup s.com...
Hi Guys,
I have a collection that contains various attributes (stuff like
strings, DateTime and Timespan) . I would like to access the collection
in various orders at different points in the application ie sometimes I
want to cycle through the values in the collection in DateTime order
while at other times in TimeSpan order.
Ideally I would like multiple keys - such as Timespan within DateTime
order but maybe that is asking too much.
Also some of the keys I would like to use are not unique.
So I guess I have three questions
1. Whats the best way of sorting a collection in general
2. Is there anyway to sort with multiple keys
3. Is there any way to do sorts with non-unique keys
Would really appreciate any info you guys could give me.
Regards,
Gary
Nov 12 '06 #4
Read them sequentially, or make chains, i.e.,
have a field that refers key x to the next key x.
Adrian.

<ga*******@gmai l.comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
But a sorted list requires a unique key right?

Any options for non unique keys?

Gary

On Nov 12, 10:31 pm, "Cor Ligthert [MVP]" <notmyfirstn... @planet.nl>
wrote:
Gary,

You can use a sorted list.

If you have more keys, you can create more in a way that there is one
that
holds the data and others which holds the references to that one in the
key
value pair.

Cor

<garyho...@gmai l.comschreef in
berichtnews:11* *************** ******@h54g2000 cwb.googlegroup s.com...
Hi Guys,
I have a collection that contains various attributes (stuff like
strings, DateTime and Timespan) . I would like to access the
collection
in various orders at different points in the application ie sometimes
I
want to cycle through the values in the collection in DateTime order
while at other times in TimeSpan order.
Ideally I would like multiple keys - such as Timespan within DateTime
order but maybe that is asking too much.
Also some of the keys I would like to use are not unique.
So I guess I have three questions
1. Whats the best way of sorting a collection in general
2. Is there anyway to sort with multiple keys
3. Is there any way to do sorts with non-unique keys
Would really appreciate any info you guys could give me.
Regards,
Gary

Nov 12 '06 #5
Re non unique keys

Another option might be to write a binary sort routine.
Then go for the key and see if there are any data items
before or after the retrieved data item that have the same
key value, and also extract those data items.

Adrian.
"Adrian <" <no*@all.access iblewrote in message
news:45******** ************@dr eader2.news.tis cali.nl...
Read them sequentially, or make chains, i.e.,
have a field that refers key x to the next key x.
Adrian.

<ga*******@gmai l.comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
But a sorted list requires a unique key right?

Any options for non unique keys?

Gary

On Nov 12, 10:31 pm, "Cor Ligthert [MVP]" <notmyfirstn... @planet.nl>
wrote:
Gary,
>
You can use a sorted list.
>
If you have more keys, you can create more in a way that there is one
that
holds the data and others which holds the references to that one in
the
key
value pair.
>
Cor
>
<garyho...@gmai l.comschreef in
berichtnews:11* *************** ******@h54g2000 cwb.googlegroup s.com...
>
Hi Guys,
>
I have a collection that contains various attributes (stuff like
strings, DateTime and Timespan) . I would like to access the
collection
in various orders at different points in the application ie
sometimes
I
want to cycle through the values in the collection in DateTime order
while at other times in TimeSpan order.
>
Ideally I would like multiple keys - such as Timespan within
DateTime
order but maybe that is asking too much.
>
Also some of the keys I would like to use are not unique.
>
So I guess I have three questions
>
1. Whats the best way of sorting a collection in general
2. Is there anyway to sort with multiple keys
3. Is there any way to do sorts with non-unique keys
>
Would really appreciate any info you guys could give me.
>
Regards,
>
Gary


Nov 12 '06 #6
ga*******@gmail .com wrote:
Hi Guys,

I have a collection that contains various attributes (stuff like
strings, DateTime and Timespan) . I would like to access the collection
in various orders at different points in the application ie sometimes I
want to cycle through the values in the collection in DateTime order
while at other times in TimeSpan order.

Ideally I would like multiple keys - such as Timespan within DateTime
order but maybe that is asking too much.

Also some of the keys I would like to use are not unique.

So I guess I have three questions

1. Whats the best way of sorting a collection in general
Depends on your constraints and requirements, such as those you indicate below.
2. Is there anyway to sort with multiple keys
Absolutely - but I think you mean compound keys rather than multiple. Here is
one from a real-world application which works by defining a class (or struct) to
contain the data members of the compound key and then implements IComparable to
provide a custom sort:

/// <summary>
/// CategorySetMemb erKey: sorts by category id within category set id
/// </summary>
public class CSMKey : IComparable
{
public long CategorySetID;
public long CategoryID;

public CSMKey(long setid, long catid)
{
this.CategorySe tID = setid;
this.CategoryID = catid;
}

public int CompareTo(objec t obj)
{
int highorder = this.CategorySe tID.CompareTo(( (CSMKey)obj).Ca tegorySetID);
if(highorder != 0)
return highorder;
else
return this.CategoryID .CompareTo(((CS MKey)obj).Categ oryID);
}
}

The resulting compound key can be used as the key portion of any IDictionary
(e.g., Hashtable or SortedList) as long as uniqueness of the overall compound
key is maintained.

Notes:
1. The compounding can easily be extended to more than 2 levels.
2. In the above example both members happen to be long ints, but the types of
the members can be mixed in any way as long as each member is a type which is
IComparable. (In particular, they could be strings or DateTime structs or
TimeSpan structs.)
3. Is there any way to do sorts with non-unique keys
One obvious trick would be similar to the above - make a unique compound key by
appending a sequence number in a lower-order member. Depending on your
requirements that may suffice.
HTH,
-rick-
Nov 12 '06 #7
Gary,

Than take the datatable in combination with the defaultview (inbuild
dataview).

You can than sort on every field in every direction and in every
combination.

Cor

<ga*******@gmai l.comschreef in bericht
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
But a sorted list requires a unique key right?

Any options for non unique keys?

Gary

On Nov 12, 10:31 pm, "Cor Ligthert [MVP]" <notmyfirstn... @planet.nl>
wrote:
>Gary,

You can use a sorted list.

If you have more keys, you can create more in a way that there is one
that
holds the data and others which holds the references to that one in the
key
value pair.

Cor

<garyho...@gma il.comschreef in
berichtnews:11 *************** *******@h54g200 0cwb.googlegrou ps.com...
Hi Guys,
I have a collection that contains various attributes (stuff like
strings, DateTime and Timespan) . I would like to access the collection
in various orders at different points in the application ie sometimes I
want to cycle through the values in the collection in DateTime order
while at other times in TimeSpan order.
Ideally I would like multiple keys - such as Timespan within DateTime
order but maybe that is asking too much.
Also some of the keys I would like to use are not unique.
So I guess I have three questions
1. Whats the best way of sorting a collection in general
2. Is there anyway to sort with multiple keys
3. Is there any way to do sorts with non-unique keys
Would really appreciate any info you guys could give me.
Regards,
Gary

Nov 12 '06 #8
Hi Rick,
The resulting compound key can be used as the key portion of any IDictionary
(e.g., Hashtable or SortedList) as long as uniqueness of the overall
compound key is maintained.
Your CSMKey class cannot be used to key a Hashtable or Dictionary unless you
make it a struct. Or you can just override Equals and GetHashCode; although,
if you override Equals then CSMKey should probably be an immutable struct
anyway.

Your current implementation will work with a SortedList because that class
doesn't use the GetHashCode method - it requires its keys to implement
IComparable. Hashtable and the generic Dictionary class (2.0 framework) do
not use IComparable implementations , they use the GetHashCode and Equals
methods.

--
Dave Sexton

"Rick Lones" <Wr******@Ychar terZ.netwrote in message
news:uT******** ***@newsfe02.lg a...
ga*******@gmail .com wrote:
>Hi Guys,

I have a collection that contains various attributes (stuff like
strings, DateTime and Timespan) . I would like to access the collection
in various orders at different points in the application ie sometimes I
want to cycle through the values in the collection in DateTime order
while at other times in TimeSpan order.

Ideally I would like multiple keys - such as Timespan within DateTime
order but maybe that is asking too much.

Also some of the keys I would like to use are not unique.

So I guess I have three questions

1. Whats the best way of sorting a collection in general

Depends on your constraints and requirements, such as those you indicate
below.
>2. Is there anyway to sort with multiple keys

Absolutely - but I think you mean compound keys rather than multiple. Here
is one from a real-world application which works by defining a class (or
struct) to contain the data members of the compound key and then implements
IComparable to provide a custom sort:

/// <summary>
/// CategorySetMemb erKey: sorts by category id within category set id
/// </summary>
public class CSMKey : IComparable
{
public long CategorySetID;
public long CategoryID;

public CSMKey(long setid, long catid)
{
this.CategorySe tID = setid;
this.CategoryID = catid;
}

public int CompareTo(objec t obj)
{
int highorder =
this.CategorySe tID.CompareTo(( (CSMKey)obj).Ca tegorySetID);
if(highorder != 0)
return highorder;
else
return this.CategoryID .CompareTo(((CS MKey)obj).Categ oryID);
}
}

The resulting compound key can be used as the key portion of any IDictionary
(e.g., Hashtable or SortedList) as long as uniqueness of the overall
compound key is maintained.

Notes:
1. The compounding can easily be extended to more than 2 levels.
2. In the above example both members happen to be long ints, but the types
of the members can be mixed in any way as long as each member is a type
which is IComparable. (In particular, they could be strings or DateTime
structs or TimeSpan structs.)
>3. Is there any way to do sorts with non-unique keys

One obvious trick would be similar to the above - make a unique compound key
by appending a sequence number in a lower-order member. Depending on your
requirements that may suffice.
HTH,
-rick-

Nov 12 '06 #9
Dave Sexton wrote:
Hi Rick,
>The resulting compound key can be used as the key portion of any IDictionary
(e.g., Hashtable or SortedList) as long as uniqueness of the overall
compound key is maintained.

Your CSMKey class cannot be used to key a Hashtable or Dictionary unless you
make it a struct. Or you can just override Equals and GetHashCode; although,
if you override Equals then CSMKey should probably be an immutable struct
anyway.

Your current implementation will work with a SortedList because that class
doesn't use the GetHashCode method - it requires its keys to implement
IComparable. Hashtable and the generic Dictionary class (2.0 framework) do
not use IComparable implementations , they use the GetHashCode and Equals
methods.
Hi Dave,

Wow, I had no idea. I just reviewed a module of mine which holds some three
dozen collections - Arraylists, Hashtables and SortedLists - and indeed the
compound key trick was only used with some of the SortedLists. In hindsight it
makes total sense of course that IComparable would not be relevant to Hashtable
keys.

Thanks for pointing this out, you probably saved me some head-scratching time in
the future . . .

Regards,
-rick-
Nov 13 '06 #10

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

Similar topics

7
3271
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)
4
2549
by: John Bullock | last post by:
Hello, I am at wit's end with an array sorting problem. I have a simple table-sorting function which must, at times, sort on columns that include entries with nothing but a space (@nbsp;). I want all of the spaces to be put in the first slots of the array. IE 6 does this. But Firefox 0.9.1 doesn't, and I don't know why. I have not been able to reproduce it in very simple form (which is itself a puzzle). But example code is...
22
4185
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
8
2274
by: nidhog | last post by:
Hello guys, I made a script that extracts strings from a binary file. It works. My next problem is sorting those strings. Output is like: ---- snip ---- 200501221530
12
5683
by: CJM | last post by:
How can I dynamically sort the results from a Stored Procedure? Or more importantly, what is the fastest and most efficient way? I know I can do the sorting within the recordset in ASP, but AFAIK this is not the most efficient method. Ideally, I'd like to pass a parameter to the SP to indicate sorting field order... How do you guys go about this?
20
4096
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: li=;
11
2157
by: rkbnair | last post by:
I have created a datagrid in my aspx with the 'AllowSorting' property to true. When clicking on the column header, the page refreshes. However the sorting is not done. Am I missing anything? I populate the data if !postback.
19
25482
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to sort columns based on the column header the user has clicked in both Ascending and Descending formats.
6
2267
by: Dennis Gearon | last post by:
This is what has to be eventually done:(as sybase, and probably others do it) http://www.ianywhere.com/whitepapers/unicode.html I'm not sure how that will affect LIKE and REGEX. ---------------------------(end of broadcast)--------------------------- TIP 5: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faqs/FAQ.html
5
1764
by: ^cypis^ vel. SQ9JTI | last post by:
Hi there, i have a problem. I prepared bucket sorting with reading values from file (data.txt) and saving to other file (aus.txt). Everything is good if we talk about reading and sorting, but saving doesn't work. data.txt looks: 4 3 67 15
0
9839
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10564
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...
0
10268
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...
0
9396
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7806
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
6998
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();...
1
4474
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
2
4039
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3123
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.