Hi. I am using a comparer class to sort a list of People details by their firstName.
The code to do this is as follows:

public class PersonDetailsComparer : IComparer<PersonDetails>
    {
        public int Compare(PersonDetails x, PersonDetails y)
        {
            int returnValue = 0;
            if (x != null && y != null && x. FirstName != null && y. FirstName != null)
            {

                    return x.FirstName.CompareTo(y.FirstName );
            }
            return returnValue;
        }
    }

This is working fine, that is the list is sorted in ascending order. But now, I need to add some exceptions, for example I want the following names to appear first from the list:
Lever
Johnson
Zaher

and then sorting the remaining list in ascending order.
Can any one please help me.
Thanks

Recommended Answers

All 6 Replies

Add some conditions. When x is one of those return 1, if y is return -1. Add more checks if they need to be sorted too.

Can you provide me with an example please.

I believe @Pritaeas means the following:

public class PersonDetailsComparer : IComparer<PersonDetails>
    {
        public int Compare(PersonDetails x, PersonDetails y)
        {
            if (x.FirstName == "Lever")
                return 1;
            if (y.FirstName == "Lever")
                return -1;

            int returnValue = 0;
            if (x != null && y != null && x. FirstName != null && y. FirstName != null)
            {
                    return x.FirstName.CompareTo(y.FirstName );
            }
            return returnValue;
        }
    }

Hi. I tried your example but then the list returned after comparison is null.

Then you did it wrong, post your code

Hi. I have been able to sort the list by creating the comparer class as follows

public class PersonDetailsComparer: IComparer< PersonDetails >
    {
        #region Private fields
        private List<string> fixedNames;
        #endregion

        #region Constructor
        public PersonDetailsComparer (List<string> fixedNames)
        {
            this.fixedNames = fixedNames;
        }
        #endregion


        public int Compare(PersonDetails x, PersonDetails y)
        {
            int returnValue = 0;

            if (x != null && y != null && x.FirstName!= null && y.FirstName!= null)
            {
                //find index of x and y from fixed list
                int indexOfXOnFixedNames = -1;
                int indexOfYOnFixedNames = -1;
                if (fixedNames!= null)
                {
                    /* checks if x.FirstName is available in the fixedNames list
                       if yes then returns the index of where it is found
                       if not, then -1 is returned */
                    indexOfXOnFixedNames  = fixedNames.IndexOf(x.FirstName);
                    indexOfYOnFixedNames  = fixedNames.IndexOf(y.FirstName);
                }

                /*any index with -1, adjust it to a bigger value
                 that is add it to the end of the fixedNames list*/
                if (indexOfXOnFixedNames  == -1)
                {
                    indexOfXOnFixedNames  = fixedNames.Count;
                }
                if (indexOfYOnFixedNames  == -1)
                {
                    indexOfYOnFixedNames  = fixedNames.Count;
                }

                /*when x is found in the fixedNames list but y isn't
                  when y is found in the fixedNames list but x isn't */
                if (indexOfXOnFixedNames != indexOfYOnFixedNames)
                {
                    return indexOfXOnFixedNames.CompareTo(indexOfYOnFixedNames);
                }
                else
                {
                    /*when both x and y are not found in the fixedNames list
                    compare the first name */
                    return x.FirstName.CompareTo(y.FirstName);
                }
            }
            return returnValue;
        }
    }

where fixedNames is the list of names which I want to appear at the top of the list.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.