sort this thing

A

Alexander Widera

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();
}
}
}
 
G

Guest

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
 
A

Alexander Widera

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 said:
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 said:
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();
}
}
}
 
A

Alexander Widera

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 said:
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 said:
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();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top