I am writing a program to set a sequence in which various objects will appear in report. The sequence is the Y position (cell) on Excel spreadsheet.
A demo part of code is below. What I want to accomplish is to have a collection, which will allow me to add multiple objects and I can get a sorted collection based on the sequence
SortedList list = new SortedList();  Header h = new Header(); h.XPos = 1; h.name = "Header_1"; list.Add(h.XPos, h);  h = new Header(); h.XPos = 1; h.name = "Header_2"; list.Add(h.XPos, h);  I know that the SortedList will not allow this and I have been searching for alternate. I don't want to eliminate the duplicates and already tried List<KeyValuePair<int, object>>.
Thanks.
Meskipun C dibuat untuk memprogram sistem dan jaringan komputer namun bahasa ini juga sering digunakan dalam mengembangkan software aplikasi. C juga banyak dipakai oleh berbagai jenis platform sistem operasi dan arsitektur komputer, bahkan terdapat beberepa compiler yang sangat populer telah tersedia.
C adalah huruf ketiga dalam alfabet Latin. Dalam bahasa Indonesia, huruf ini disebut ce (dibaca [tʃe]).
Bahasa pemrograman C ini dikembangkan antara tahun 1969 – 1972 oleh Dennis Ritchie. Yang kemudian dipakai untuk menulis ulang sistem operasi UNIX. Selain untuk mengembangkan UNIX, bahasa C juga dirilis sebagai bahasa pemrograman umum.
Use your own IComparer!
Like already stated in some other answers, you should use your own comparer class. For this sake I use a generic IComparer class, that works with anything that implements IComparable:
/// <summary> /// Comparer for comparing two keys, handling equality as beeing greater /// Use this Comparer e.g. with SortedLists or SortedDictionaries, that don't allow duplicate keys /// </summary> /// <typeparam name="TKey"></typeparam> public class DuplicateKeyComparer<TKey>                 :              IComparer<TKey> where TKey : IComparable {     #region IComparer<TKey> Members      public int Compare(TKey x, TKey y)     {         int result = x.CompareTo(y);          if (result == 0)             return 1; // Handle equality as being greater. Note: this will break Remove(key) or         else          // IndexOfKey(key) since the comparer never returns 0 to signal key equality             return result;     }      #endregion }  You will use it when instancing a new SortedList, SortedDictionary etc:
SortedList<int, MyValueClass> slist = new SortedList<int, MyValueClass>(new DuplicateKeyComparer<int>());  Here int is the key that can be duplicate.
You can safely use List<> . The List has a Sort method , an overload of which accepts IComparer. You can create your own sorter class as . Here's an example :
private List<Curve> Curves; this.Curves.Sort(new CurveSorter());  public class CurveSorter : IComparer<Curve> {     public int Compare(Curve c1, Curve c2)     {         return c2.CreationTime.CompareTo(c1.CreationTime);     } } 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With