Click here to Skip to main content
16,004,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In My application, I have stored employ names in a List<Feature>.
I have implemented Compare Method of IComparer interface to implemenet sorting as follows.

public int Compare(firstEmPName, secondEmpName)
        {
            int diffResult = 0;
diffResult = string.Compare(firstEmPName, secondEmpName);
            return diffResult;
        } 


I have shown the simple example but the actual sorting is quite complex. Hence I have used IComparer .

Now during sorting I want to keep, some names ( e.g.: nameFirst and nameLast) at the first and last location , and other names should get sorted as alphabetically .

How to customize sorting for such a scenario?
Posted

In the class that implements Compare you need to add two fields/properties:
C#
private Feature _alwaysFirst;
private Feature _alwaysLast;

And do this in your comparer:
C#
public int Comparer(Feature left, Feature right)
    {
    if (left == _alwaysFirst)
        return 1;
    if (left == _alwaysLast)
        return -1;
    return string.Compare(left.FirstName, right.FirstName);
    }

This architecture allows you to select just one Feature (employee) to appear first, and just one to appear last.

Note that names are not unique, so I don't recommend making _alwaysFirst and _alwaysLast strings, and comparing against left.FirstName. Sure, it would group all elements of the list which have identical first/last names that match the _alwaysXXX names at the beginning/end, but it wouldn't sort within those groups (well, you'd have do some other compare instead of returning 1/-1 to get sorting within those groups).
 
Share this answer
 
I would just add one more field in your "Feature" class definition to hold the score of the instance and modify your code as

C#
public int Compare(firstEmp, secondEmp)
        {
            int diffResult = 0;
diffResult = string.Compare(firstEmp.FirstName, secondEmp.FirstName)*(firstEmp.Score-secondEmp.Score);
            return diffResult;
        }


in simple terms, add one more valuation criteria on your objects so that comparing can be more dynamic than just one name on the top and one at the bottom. what ever names you want to put on the top of you sorting give them a bigger score and the same for the ones you want keep to the bottom. and keep the default 0 so that it won't affect sorting.

good luck
 
Share this answer
 
Comments
Chris Trelawny-Ross 1-Sep-10 15:24pm    
If two items being compared score equally, won't that suppress the sorting on FirstName? The OP also indicated that his sample was simplified compared to his actual Compare routine ... so perhaps adding a trigger on, say, a non-zero score before the score is actually used in the comparison?
I dont know why dont you employ LINQ into place.

You can easily use

collection.OrderBy
which is new concept and doesnt need to implement IComparer.
 
Share this answer
 
Comments
Chris Trelawny-Ross 1-Sep-10 16:59pm    
Reason for my vote of 1
a. This question was about how to do the first/last thing, so using Linq OrderBy does not address the question at all.
b. Linq OrderBy would not allow the OP to force items into first and last places
c. The OP indicated that his compare routine is significantly more complex than his sample, which would make for an overly large anonymous method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900