Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I sort on lastname?

Posted on 2009-07-11
7
Medium Priority
?
295 Views
Last Modified: 2012-05-07
I would like to sort on lastname and second on firstname.

how?

I would like to use IComparable
ContactManager.cs
--------------------------------
using System;
using System.Collections.Generic;
using System.Text;
 
namespace Persons
{
    [Serializable]
    public class ContactManager
    {
 
        //Deklaration
        List<Contact> contact;
        bool exists = false;
 
        #region Konstruktor
 
 
 
        public ContactManager()
        {
            contact = new List<Contact>();           
            exists = true;
        } //end CustomerManager
 
 
 
         public ContactManager(List<Contact> constact)
        {
            this.contact = constact;
        } //end CustomerManager
 
        #endregion
        
 
        #region Egenskaper
 
 
 
        public int Count
        {
            get { return contact.Count; }
        } //end Count
 
 
 
        public List<Contact> Contacts
        {
            get { return contact; }
            set { contact = value; }
        } //end Customers
 
 
        #endregion
 
 
        #region Metoder
 
 
 
        public int Search(Contact cust)
        {
            int found = -1;
 
            if (exists)
            {
                for (int i = 0; i < contact.Count; i++)
                {
                    if (contact[i].FirstName.Equals(cust.FirstName) && contact[i].LastName.Equals(cust.LastName))
                    {
                        found = i;
                    } //end if                                    
                } //end for
            } //end if
 
 
            return found;
        } //end Search
 
 
 
        public List<Contact> FindALetterSearch(char letter)
        {
 
            List<Contact> returnList = new List<Contact>();
 
            if (exists)
            {
                returnList = contact.FindAll(c => c.LastName.Trim().ToLowerInvariant().StartsWith(letter.ToString().Trim().ToLowerInvariant()));
 
            } //end if            
            return returnList;
 
 
            
        } //end Search
 
 
        public void DeleteCustomer (int index) 
        {
            if (exists)
            {
                contact.RemoveAt(index);                
            } //end if        
        } //end DeleteCustomer
 
 
 
        public void ChangeCustomer(Contact cust)
        {
 
        }
 
 
        public Contact GetCustomer(int index)
        {
            return null;
        } //end GetCustomer
 
 
 
 
        public void AddCustomer(Contact cust)
        {
            if(exists)
            {
                contact.Add(cust);
            } //end if
 
            else
            {
                contact = new List<Contact>();
                contact.Add(cust);
                exists = true;
            } //end else            
        } //end AddCustomer
 
 
 
 
 
        #endregion
 
    } //end class
} //end namespace
 
 
-------------------------------
Contact.cs
----------------------------
 
using System;
using System.Collections.Generic;
using System.Text;
 
namespace Persons
{
    [Serializable]
    public class Contact : IContact
    {
        #region Attribut
 
        private string m_firstName;
        private string m_lastName;
        private List<Phone> m_phone;
        private Address m_adress;
        private Email m_email;
        private static int m_id;
        private int thiscount;
 
        #endregion
 
 
        #region Konstruktor
 
 
 
        //*******************************************************************
        //          Contact
        //*******************************************************************
        public Contact()
        {
            m_adress = new Address ();
            m_email = new Email ();
            m_lastName = string.Empty;
            m_firstName = string.Empty;
            m_phone = new List<Phone> ();
 
            thiscount = m_id++;
        } //end Contact
 
 
 
        //*******************************************************************
        //          Contact
        //*******************************************************************
        public Contact(string name, string lastname, Address adress, List<Phone> phone, Email email)
        {
            m_adress = adress;
            m_email = email;
            m_lastName = lastname;
            m_firstName = name;
            m_phone = phone;
 
            thiscount = m_id++;
        } //end Contact
 
        #endregion
 
 
 
        #region Egenskaper
 
 
        //*******************************************************************
        //          MyAdress
        //*******************************************************************
        public Address MyAdress
        {
            get { return m_adress; }
            set { m_adress = value; }
        } //end MyAdress
 
 
 
        //*******************************************************************
        //          Id
        //*******************************************************************
        public int Id
        {
            get { return thiscount; }
            set { thiscount = value; }
        } //end Id
 
 
 
        //*******************************************************************
        //          MyEmail
        //*******************************************************************
        public Email MyEmail
        {
            get { return m_email; }
            set { m_email = value; }
        } //end MyEmail
 
 
        //*******************************************************************
        //          FirstName
        //*******************************************************************
        public string FirstName
        {
            get { return m_firstName; }
            set { m_firstName = value; }
        } //end FirstName
 
 
        //*******************************************************************
        //          LastName
        //*******************************************************************
        public string LastName
        {
            get { return m_lastName; }
            set { m_lastName = value; }
        } //end LastName
 
 
        //*******************************************************************
        //          Telephone
        //*******************************************************************
        public List<Phone> Telephone
        {
            get { return m_phone; }
            set { m_phone = value; }
        } //end Telephone
 
 
 
        #endregion
 
 
 
        #region Metoder
 
 
        //*******************************************************************
        //          GetContact
        //*******************************************************************
        public string GetContact()
        {
            return m_lastName;
        } //end GetContact
 
 
 
        //*******************************************************************
        //          ToString
        //*******************************************************************
        public override string ToString()
        {
            return "" + m_firstName + m_lastName + m_adress.ToString() + m_phone.ToString() + m_email.ToString();
        } //end ToString
 
        #endregion
 
 
        #region IContact Members
 
 
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fname"></param>
        public void setFirstName(string fname)
        {
            this.FirstName = fname;
        }
 
 
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="lname"></param>
        public void setLastName(string lname)
        {
            this.LastName = lname;
        }
 
        #endregion
    } //end class
} //end namespace

Open in new window

0
Comment
Question by:Mickeys
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 24830396
What is it that you want sorted?
0
 

Author Comment

by:Mickeys
ID: 24830450
my contact.last name   (first)
and then second if there is the same lastname.......it should sort on firstname
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 24830522
How you want it sorted is clear, but what is it that you want sorted?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:Mickeys
ID: 24830559
I have a contactManager
In this there is a contact List.

This list is what I want sorted
0
 

Author Comment

by:Mickeys
ID: 24830565
To be specifc:   List<Contact> contact;
in ContactManager.cs
0
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 2000 total points
ID: 24830676
I suggest that you use the interface IComparable<Contact> instead of the interface IComparable.

Make the Contact class implement the interface, and it's CompareTo method:

class Contact : IComparable<Contact> {

   public int CompareTo(Contact other) {
      int result = this.LastName.CompareTo(other.LastName);
      if (result == 0) {
         result = this.FirstName.CompareTo(other.FirstName);
      }
      return result;
   }

   ...

}

Then you can easily sort the contacts:

contact.Sort();
0
 

Author Closing Comment

by:Mickeys
ID: 31602417
Ahhh now I see. Thx it works great as well
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question