Computers And Industry

SortedSet Example In C#


The following example shows how to use a sortedset in C#. This sample simply prints out the contents of the sorted set. Also note that this class can be used in any project type (ie Net, AspNetCore, etc. The samples use a console application to demonstrate the class.

Using the Class


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Samples
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("***BEGIN SAMPLE OUTPUT***\n\n");
            SortedSetSample obj = new SortedSetSample();
            obj.Show();


            Console.WriteLine("\n\n***END SAMPLE OUTPUT***");
            Console.ReadLine();
        }
    }
}  // END PROGRAM



The Sorted Set Sample Class


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace Samples
{
    class SortedSetSample
    {
        private SortedSetlstHumanBeings = new SortedSet(new SortByAge());

      // ---------------------------------
    // sorted set constructor
      // ---------------------------------
      public SortedSetSample()
        {
            human h = new human("bob", "smith", 49);
            lstHumanBeings.Add(h);

            h = new human("tina", "jones", 24);
            lstHumanBeings.Add(h);

            h = new human("biff", "thorn", 31);
            lstHumanBeings.Add(h);

            h = new human("tina", "jones", 16);
            lstHumanBeings.Add(h);

        }

        // ------------------------------------
        // shows the list in the debug window
        // ------------------------------------
        public void Show()
        {
            Console.WriteLine("Output is sorted by Last, First, Age");
            Console.WriteLine("");
            foreach (human h in lstHumanBeings)
            {
                string s = h.last.PadRight(10) +
                           h.first.PadRight(10) +
                           h.age.ToString().PadLeft(10);
                Console.WriteLine(s);
            }
        }



        // --------------------------------
        // human class
        // --------------------------------
        public class human
        {
            public string first { get; set; }
            public string last { get; set; }
            public int age { get; set; }

            // constructor - 0 parameters
            public human()
            {
                first = "";
                last = "";
                age = 0;
            }

            // constructor - 3 parameters
            public human(string p_first, string p_last, int p_age)
            {
                first = p_first;
                last = p_last;
                age = p_age;
            }
        }


        // sort object.  note that the combination of 
        // first, last, and age must be unique
        private class SortByAge : IComparer
        {
            public int Compare(human h1, human h2)
            {
                int rc = string.Compare(h1.last, h2.last);
                if (rc == 0)
                {
                    rc = string.Compare(h1.first, h2.first);

                    if (rc == 0)
                    {
                        rc = h1.age.CompareTo(h2.age);
                    }
                }

                return rc;
            }
        }
    }
}  // END CLASS


Open Source

Paul F. Sirpenski
Personal Open Source Directory Of Paul F. Sirpenski

ASP.NET Core
Open Source directory Of the Microsoft Asp.Net Core project.

Developed By Paul F. Sirpenski. Copyright 2022.