Jawad Khan

Jawad's Lodge - The willingness to torture yourself before others is what makes a developer truly a unique breed.
posts - 45, comments - 133, trackbacks - 155

My Links

News

Archives

Post Categories

Image Galleries

SortedList that sorts By Value instead of Key ...

During my consultancy engagements I have come across numerous time when you need to sort the value displayed in the drop down box or in the the other Data bound control. It can be very frustrating to do that if you don't have much experience with .Net and you have to repeat similar code again and again. Well .Net 2.0 will ease it quite a bit with Generics ... For now here is a class that works exactly like sorted list but sorts the List by Value instead of Key ...

This is meant for reasonable size list but not very huge lists. There will be performance degradation for real huge lists. I found the code from Code Project or some other site and forgot the name of the author so If you know about him put this in the feedback and I be more then happy to post it in this Post. I made a few changes to it to make it a little more efficient.

  Just create a Class library and add the following Class.

Usage ...

               using  Jawad.Utils.Collection;
               ..................

              LookupCoolection myColl = new LookupCollection();
              myColl.Add(“Key1“, “Oracle“)
              myCollAdd(“Key2“, “Microsoft“);
              myDropDown.DataSource = MyColl;
              myDropDown.DataBind();

             The Dropdown now will show Microsoft first and Oracle second unlike if we have used Sorted list.

 using System;
 using System.Collections;
    using Cdi.Ces.Utils.Collection.internalUse;

 namespace Jawad.Utils.Collection
 {

  [Serializable]
  public class LookupCollection : ICollection, IDictionary, IEnumerable
  {
   private ArrayList mItems = new ArrayList();

   //---------------------------------------------------------------------
   // Constructor
   //---------------------------------------------------------------------
   public LookupCollection()
   {
   }

   //---------------------------------------------------------------------
   // Add
   //---------------------------------------------------------------------
   public void Add(object key, object value)
   {
    // do some validation
    if (key == null)
     throw new ArgumentNullException("key is a null reference");
    else if (this.Contains(key))
     throw new
      ArgumentException("An element with the same key already exists");

    // add the new item
    Lookup newItem = new Lookup();
   
    newItem.Key   = key;
    newItem.Value = value;

    this.mItems.Add(newItem);
   }

   //---------------------------------------------------------------------
   // Clear
   //---------------------------------------------------------------------
   public void Clear()
   {
    this.mItems.Clear();
   }

   //---------------------------------------------------------------------
   // Contains
   //---------------------------------------------------------------------
   public bool Contains(object key)
   {
    return (this.GetByKey(key) != null);
   }

   //---------------------------------------------------------------------
   // CopyTo
   //---------------------------------------------------------------------
   public void CopyTo(Array array, int index)
   {
    this.mItems.CopyTo(array, index);
   }

   //---------------------------------------------------------------------
   // GetEnumerator (1)
   //---------------------------------------------------------------------
   public IDictionaryEnumerator GetEnumerator()
   {
    this.mItems.Sort();
    return new LookupEnumerator(this.mItems);
   }

   //---------------------------------------------------------------------
   // GetEnumerator (2)
   //---------------------------------------------------------------------
   IEnumerator IEnumerable.GetEnumerator()
   {
    this.mItems.Sort();
    return new LookupEnumerator(this.mItems);
   }

   //---------------------------------------------------------------------
   // Remove
   //---------------------------------------------------------------------
   public void Remove(object key)
   {
    if (key == null)
     throw new ArgumentNullException("key is a null reference");

    Lookup deleteItem = this.GetByKey(key);
    if (deleteItem != null)
    {
     this.mItems.Remove(deleteItem);
    }
   }

   //=====================================================================
   // PRIVATE
   //=====================================================================
   private Lookup GetByKey(object key)
   {
    Lookup    result   = null;
    int       keyIndex = -1;
    ArrayList keys     = (ArrayList)this.Keys;

    if (this.mItems.Count > 0)
    {
     keyIndex = keys.IndexOf(key);

     if (keyIndex >= 0)
     {
      result = (Lookup)this.mItems[keyIndex];
     }
    }

    return result;
   }

   //=====================================================================
   // PROPERTIES
   //=====================================================================
   public int Count
   {
    get
    {
     return this.mItems.Count;
    }
   }
       
   public bool IsSynchronized
   {
    get
    {
     return false;
    }
   }

   public object SyncRoot
   {
    get
    {
     return this;
    }
   }

   public bool IsFixedSize
   {
    get
    {
     return false;
    }
   }

   public bool IsReadOnly
   {
    get
    {
     return false;
    }
   }

   public object this[object key]
   {
    get
    {

     if (key == null)
      throw new ArgumentNullException("key is a null reference");

     object result = null;

     Lookup findItem = this.GetByKey(key);
     if (findItem != null)
     {
      result = findItem.Value;
     }

     return result;
    }
    set
    {
    }
   }

   public ICollection Keys
   {
    get
    {
     ArrayList result = new ArrayList();

     this.mItems.Sort();

     foreach (Lookup curItem in this.mItems)
     {
      result.Add(curItem.Key);
     }

     return result;
    }
   }

   public ICollection Values
   {
    get
    {
     ArrayList result = new ArrayList();
     
     this.mItems.Sort();

     foreach (Lookup curItem in this.mItems)
     {
      result.Add(curItem.Value);
     }

     return result;
    }
   }
  }
 }

Print | posted on Tuesday, May 17, 2005 10:10 AM | Filed Under [ ASP.NET ]

Feedback

Gravatar

# re: SortedList that sorts By Value instead of Key ...

The code won't compile until you define the class "Lookup". Author: please update your code to include this!
3/14/2006 1:32 PM | It's Just Me.
Gravatar

# re: SortedList that sorts By Value instead of Key ...

Here you Go. Sorry for Missing Code ....
using System;
using System.Collections;

namespace Jawad.Utils.Collection.internalUse
{
[Serializable]
internal class Lookup : IComparable
{
private object mKey;
private object mValue;

//---------------------------------------------------------------------
// Default Constructor
//---------------------------------------------------------------------
public Lookup() : this(null, null)
{
}

//---------------------------------------------------------------------
// Overloaded Constructor
//---------------------------------------------------------------------
public Lookup(object key, object value)
{
this.Key = key;
this.Value = value;
}

//---------------------------------------------------------------------
// CompareTo
//---------------------------------------------------------------------
public int CompareTo(object obj)
{
int result = 0;

if (obj is Lookup)
{
result =
((IComparable)this.Value).CompareTo((IComparable)
(((Lookup)obj).Value));
}

return result;
}

//---------------------------------------------------------------------
// ToDictionaryEntry
//---------------------------------------------------------------------
public DictionaryEntry ToDictionaryEntry()
{
return new DictionaryEntry(this.Key, this.Value);
}

//=====================================================================
// PROPERTIES
//=====================================================================
public object Key
{
get
{
return this.mKey;
}
set
{
if (this.mKey != value)
{
this.mKey = value;
}
}
}

public object Value
{
get
{
return this.mValue;
}
set
{
if (this.mValue != value)
{
this.mValue = value;
}
}
}
}
}

Here is the LookUpEnumeration Class ....

using System;
using System.Collections;

namespace
Jawad.Utils.Collection.internalUse
{
internal class LookupEnumerator : IDictionaryEnumerator
{
private int index = -1;
private ArrayList items;

//---------------------------------------------------------------------
// Constructor
//---------------------------------------------------------------------
public LookupEnumerator(ArrayList list)
{
this.items = list;
}

//---------------------------------------------------------------------
// MoveNext
//---------------------------------------------------------------------
public bool MoveNext()
{
this.index++;
if (index >= this.items.Count)
return false;

return true;
}

//=====================================================================
// PROPERTIES
//=====================================================================
//---------------------------------------------------------------------
// Reset
//---------------------------------------------------------------------
public void Reset()
{
this.index = -1;
}

//---------------------------------------------------------------------
// Current
//---------------------------------------------------------------------
public object Current
{
get
{
if (this.index < 0 || index >= this.items.Count)
throw new InvalidOperationException();

return this.items[index];
}
}

//---------------------------------------------------------------------
// Entry
//---------------------------------------------------------------------
public DictionaryEntry Entry
{
get
{
return ((Lookup)this.Current).ToDictionaryEntry();
}
}

//---------------------------------------------------------------------
// Key
//---------------------------------------------------------------------
public object Key
{
get
{
return this.Entry.Key;
}
}

//---------------------------------------------------------------------
// Value
//---------------------------------------------------------------------
public object Value
{
get
{
return this.Entry.Value;
}
}
}
}
3/15/2006 7:31 AM | Jawad Khan
Gravatar

# re: SortedList that sorts By Value instead of Key ...

The databind() doesn't show the values properly, and trying to iterate over this list isn't working either. Type are the objects in the Enumerator?
7/27/2006 8:16 AM | Mark Cornelius

Post Comment

Title  
Name  
Email
Url
Comment   

Powered by: