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

sort this thing

Hi,
I have a problem with this code ... (see below) ... I want to sort an
instance of MyList ... by MyData.Shortname ... Shortname is of the type
string.... how can I sort the entries?

Thank you for your help.
Alex
using System;
using System.Collections;
using System.Collections.Specialized;

public class MyList : IEnumerable, ICollection {
protected Hashtable _myList = Hashtable.Synchronized(new Hashtable());
protected int _Version = 1;

// IEnumerable Implementation
public IEnumerator GetEnumerator() {
return(new MyListEnumerator(this));
}

// ICollection Implementation
public int Count {
get { return(_myList.Count); }
}

bool ICollection.IsSynchronized {
get { return(_myList.IsSynchronized); }
}

object ICollection.SyncRoot {
get { return(_myList.SyncRoot); }
}

void ICollection.CopyTo(Array array, int index) {
_myList.CopyTo(array, index);
}
// functions
public void Add(MyData mdata)
{
if(mdata == null) {
throw new ArgumentNullException("error: NULL !!!");
}
if(_myList.ContainsKey(mdata.DataID)) {
this[mdata.DataID].Anzahl++;
} else {
_Version++;
_myList.Add(mdata.DataID, mdata);
}
}

public void Remove(MyData mdata)
{
_Version++;
_myList.Remove(mdata.DataID);
}

public void RemoveByID(string dataid)
{
_Version++;
_myList.Remove(dataid);
}

public MyData this[string id] {
get {
_Version++;
return((MyData) _myList[id]);
}
}
/* Rest Of Implementation*/
internal class MyListEnumerator : IEnumerator {
private IDictionaryEnumerator _Enumerator;
private int _Version;
private MyList _myList;

public MyListEnumerator(MyList mylist) {
_myList = mylist;
_Enumerator = _myList._myList.GetEnumerator();
_Version = _myList._Version;
}

private void CheckVersion() {
if(_Version != _myList._Version) {
throw new InvalidOperationException("error");
}
}

public MyData Current {
get {
CheckVersion();
return((MyData) _Enumerator.Value);
}
}

object IEnumerator.Current {
get { return(Current); }
}

public bool MoveNext() {
CheckVersion();
return(_Enumerator.MoveNext());
}

public void Reset() {
CheckVersion();
_Enumerator.Reset();
}
}
}
Nov 19 '05 #1
3 1462
I assume what you are looking for is to sort the _myList hash table which
contains MyData objects

For this, you will need to implement IComparable interface on MyData

public class MyData : IComparable

and override CompareTo

public int CompareTo(Object obj)
{
if(obj is MyData)
{
MyData objData = (MyData)obj;
return(_Name.CompareTo(objData.Name));
}
return 0;
}

Hope this helps

"Alexander Widera" wrote:
Hi,
I have a problem with this code ... (see below) ... I want to sort an
instance of MyList ... by MyData.Shortname ... Shortname is of the type
string.... how can I sort the entries?

Thank you for your help.
Alex
using System;
using System.Collections;
using System.Collections.Specialized;

public class MyList : IEnumerable, ICollection {
protected Hashtable _myList = Hashtable.Synchronized(new Hashtable());
protected int _Version = 1;

// IEnumerable Implementation
public IEnumerator GetEnumerator() {
return(new MyListEnumerator(this));
}

// ICollection Implementation
public int Count {
get { return(_myList.Count); }
}

bool ICollection.IsSynchronized {
get { return(_myList.IsSynchronized); }
}

object ICollection.SyncRoot {
get { return(_myList.SyncRoot); }
}

void ICollection.CopyTo(Array array, int index) {
_myList.CopyTo(array, index);
}
// functions
public void Add(MyData mdata)
{
if(mdata == null) {
throw new ArgumentNullException("error: NULL !!!");
}
if(_myList.ContainsKey(mdata.DataID)) {
this[mdata.DataID].Anzahl++;
} else {
_Version++;
_myList.Add(mdata.DataID, mdata);
}
}

public void Remove(MyData mdata)
{
_Version++;
_myList.Remove(mdata.DataID);
}

public void RemoveByID(string dataid)
{
_Version++;
_myList.Remove(dataid);
}

public MyData this[string id] {
get {
_Version++;
return((MyData) _myList[id]);
}
}
/* Rest Of Implementation*/
internal class MyListEnumerator : IEnumerator {
private IDictionaryEnumerator _Enumerator;
private int _Version;
private MyList _myList;

public MyListEnumerator(MyList mylist) {
_myList = mylist;
_Enumerator = _myList._myList.GetEnumerator();
_Version = _myList._Version;
}

private void CheckVersion() {
if(_Version != _myList._Version) {
throw new InvalidOperationException("error");
}
}

public MyData Current {
get {
CheckVersion();
return((MyData) _Enumerator.Value);
}
}

object IEnumerator.Current {
get { return(Current); }
}

public bool MoveNext() {
CheckVersion();
return(_Enumerator.MoveNext());
}

public void Reset() {
CheckVersion();
_Enumerator.Reset();
}
}
}

Nov 19 '05 #2
hi,
i implemented now IComparable to the MyData class.... but how do I sort
MyList now?
I tried it by "converting" the hashtable to an ArrayList like this:

public ICollection Sort()
{
ArrayList sorter = new ArrayList();
sorter.AddRange(_myData);
sorter.Sort();
return sorter;
}

But I get the error "System.ArgumentException: At least one object must
implement IComparable."

Is there an other way to sort MyList?

Thanks for your help.
Alex

"Sreejith Ram" <Sr*********@discussions.microsoft.com> schrieb im
Newsbeitrag news:81**********************************@microsof t.com...
I assume what you are looking for is to sort the _myList hash table which
contains MyData objects

For this, you will need to implement IComparable interface on MyData

public class MyData : IComparable

and override CompareTo

public int CompareTo(Object obj)
{
if(obj is MyData)
{
MyData objData = (MyData)obj;
return(_Name.CompareTo(objData.Name));
}
return 0;
}

Hope this helps

"Alexander Widera" wrote:
Hi,
I have a problem with this code ... (see below) ... I want to sort an
instance of MyList ... by MyData.Shortname ... Shortname is of the type
string.... how can I sort the entries?

Thank you for your help.
Alex
using System;
using System.Collections;
using System.Collections.Specialized;

public class MyList : IEnumerable, ICollection {
protected Hashtable _myList = Hashtable.Synchronized(new
Hashtable());
protected int _Version = 1;

// IEnumerable Implementation
public IEnumerator GetEnumerator() {
return(new MyListEnumerator(this));
}

// ICollection Implementation
public int Count {
get { return(_myList.Count); }
}

bool ICollection.IsSynchronized {
get { return(_myList.IsSynchronized); }
}

object ICollection.SyncRoot {
get { return(_myList.SyncRoot); }
}

void ICollection.CopyTo(Array array, int index) {
_myList.CopyTo(array, index);
}
// functions
public void Add(MyData mdata)
{
if(mdata == null) {
throw new ArgumentNullException("error: NULL !!!");
}
if(_myList.ContainsKey(mdata.DataID)) {
this[mdata.DataID].Anzahl++;
} else {
_Version++;
_myList.Add(mdata.DataID, mdata);
}
}

public void Remove(MyData mdata)
{
_Version++;
_myList.Remove(mdata.DataID);
}

public void RemoveByID(string dataid)
{
_Version++;
_myList.Remove(dataid);
}

public MyData this[string id] {
get {
_Version++;
return((MyData) _myList[id]);
}
}
/* Rest Of Implementation*/
internal class MyListEnumerator : IEnumerator {
private IDictionaryEnumerator _Enumerator;
private int _Version;
private MyList _myList;

public MyListEnumerator(MyList mylist) {
_myList = mylist;
_Enumerator = _myList._myList.GetEnumerator();
_Version = _myList._Version;
}

private void CheckVersion() {
if(_Version != _myList._Version) {
throw new InvalidOperationException("error");
}
}

public MyData Current {
get {
CheckVersion();
return((MyData) _Enumerator.Value);
}
}

object IEnumerator.Current {
get { return(Current); }
}

public bool MoveNext() {
CheckVersion();
return(_Enumerator.MoveNext());
}

public void Reset() {
CheckVersion();
_Enumerator.Reset();
}
}
}

Nov 19 '05 #3
hi,
i implemented now IComparable to the MyData class.... but how do I sort
MyList now?
I tried it by "converting" the hashtable to an ArrayList like this:

public ICollection Sort()
{
ArrayList sorter = new ArrayList();
sorter.AddRange(_myData);
sorter.Sort();
return sorter;
}

But I get the error "System.ArgumentException: At least one object must
implement IComparable."

Is there an other way to sort MyList?

Thanks for your help.
Alex

"Sreejith Ram" <Sr*********@discussions.microsoft.com> schrieb im
Newsbeitrag news:81**********************************@microsof t.com...
I assume what you are looking for is to sort the _myList hash table which
contains MyData objects

For this, you will need to implement IComparable interface on MyData

public class MyData : IComparable

and override CompareTo

public int CompareTo(Object obj)
{
if(obj is MyData)
{
MyData objData = (MyData)obj;
return(_Name.CompareTo(objData.Name));
}
return 0;
}

Hope this helps

"Alexander Widera" wrote:
Hi,
I have a problem with this code ... (see below) ... I want to sort an
instance of MyList ... by MyData.Shortname ... Shortname is of the type
string.... how can I sort the entries?

Thank you for your help.
Alex
using System;
using System.Collections;
using System.Collections.Specialized;

public class MyList : IEnumerable, ICollection {
protected Hashtable _myList = Hashtable.Synchronized(new
Hashtable());
protected int _Version = 1;

// IEnumerable Implementation
public IEnumerator GetEnumerator() {
return(new MyListEnumerator(this));
}

// ICollection Implementation
public int Count {
get { return(_myList.Count); }
}

bool ICollection.IsSynchronized {
get { return(_myList.IsSynchronized); }
}

object ICollection.SyncRoot {
get { return(_myList.SyncRoot); }
}

void ICollection.CopyTo(Array array, int index) {
_myList.CopyTo(array, index);
}
// functions
public void Add(MyData mdata)
{
if(mdata == null) {
throw new ArgumentNullException("error: NULL !!!");
}
if(_myList.ContainsKey(mdata.DataID)) {
this[mdata.DataID].Anzahl++;
} else {
_Version++;
_myList.Add(mdata.DataID, mdata);
}
}

public void Remove(MyData mdata)
{
_Version++;
_myList.Remove(mdata.DataID);
}

public void RemoveByID(string dataid)
{
_Version++;
_myList.Remove(dataid);
}

public MyData this[string id] {
get {
_Version++;
return((MyData) _myList[id]);
}
}
/* Rest Of Implementation*/
internal class MyListEnumerator : IEnumerator {
private IDictionaryEnumerator _Enumerator;
private int _Version;
private MyList _myList;

public MyListEnumerator(MyList mylist) {
_myList = mylist;
_Enumerator = _myList._myList.GetEnumerator();
_Version = _myList._Version;
}

private void CheckVersion() {
if(_Version != _myList._Version) {
throw new InvalidOperationException("error");
}
}

public MyData Current {
get {
CheckVersion();
return((MyData) _Enumerator.Value);
}
}

object IEnumerator.Current {
get { return(Current); }
}

public bool MoveNext() {
CheckVersion();
return(_Enumerator.MoveNext());
}

public void Reset() {
CheckVersion();
_Enumerator.Reset();
}
}
}

Nov 19 '05 #4

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

Similar topics

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...
6
by: Xiaozhu | last post by:
say, if you had parallel arrays containing the sales person id, the month, and the sales figures (for that person in that month), sorting on sales figure and preserve the order of figures within...
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...
6
by: Becker | last post by:
Very simple, I have a datagrid on a windows form. I load some rows in it. I click a column header and it sorts ascending. I click it again it sorts descending. This is great, but what I want to...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
16
by: Gerrit | last post by:
Hello, Is it possible to sort an array with a struct in it? example: I have a struct: public struct mp3file { public int tracknr;
5
by: Peter Olcott | last post by:
I have used the standard template library quite a bit, and std::sort has been very useful. Now I have reached the point where my memory requirements are exceeded. Is there some standard way to sort...
10
by: Frank | last post by:
I have a text file, one word per line. I want to arrange the lines alphabetically so I opened it into MS Word and asked Word to sort it. Word said that the list was too big for it. I figure I'll...
30
by: gaoxtwarrior | last post by:
a sort which is stable means it keeps the object's original order. A sort which is in place is stable. does anyone can explain me what is sort in place? thx.
10
by: Woody Ling | last post by:
In 32 bits DB2 environment, is it meaningful to set sheapthres larger than 256MB for the following case.. 1. Intra-parallel is ON 2. Intra-parallel is OFF
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.