Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort a List and keep a particular element at end of list after sorting

I have a list of string containing "Others". I am getting this list for drop down. I am sorting this list alphabetically. But I need "Others" always at end of list. I don't want to add this element after sorting which is one solution. Is there any other way to do the same like by using custom comparer of .Sort() method. I tried like below but no solution.

 public class EOComparer : IComparer<string>
    {
        public int Compare(string x, string y)
        {
            if (x == null)
            {
                if (y == null)
                {
                    // If x is null and y is null, they're
                    // equal. 
                    return 0;
                }
                else
                {
                    // If x is null and y is not null, y
                    // is greater. 
                    return -1;
                }
            }
            else
            {
                // If x is not null...
                //
                if (y == null)
                // ...and y is null, x is greater.
                {
                    return 1;
                }
                else
                {
                    if (x.ToLower().Contains("other"))
                    {
                        return -1;
                    }
                    else
                    {
                        // If the strings are of equal length,
                        // sort them with ordinary string comparison.
                        //
                        return x.CompareTo(y);
                    }
                }
            }
        }

and calling it as :

EOComparer c = new EOComparer();
a.Sort((x, y) => c.Compare(x.OptionValue, y.OptionValue));
             return a;

Please help if it is possible.

like image 755
Gurmeet Avatar asked Mar 17 '16 10:03

Gurmeet


1 Answers

This is a fine answer, but I thought I would fix your comparer:

Test:

[TestCase(new string[0], new string[0])]
[TestCase(new[] { "a" }, new[] { "a" })]
[TestCase(new[] { "a", "b" }, new[] { "a", "b" })]
[TestCase(new[] { "b", "a" }, new[] { "a", "b" })]
[TestCase(new[] {"others"}, new[] {"others"})]
[TestCase(new[] {"a", "others"}, new[] {"a", "others"})]
[TestCase(new[] {"others", "a"}, new[] {"a", "others"})]
[TestCase(new[] {"others", "x"}, new[] {"x", "others"})]
[TestCase(new[] {"Others", "x"}, new[] {"x", "Others"})]
[TestCase(new[] { "othersz", "others" }, new[] { "othersz", "others" })]
[TestCase(new[] {"z", "y", "x", "others", "b", "a", "c"},
          new[] {"a", "b", "c", "x", "y", "z", "others"})]
public void CanSortWithOthersAtEnd(string[] input, string[] expectedSorted)
{
    var a = new List<string>(input);
    var c = new EOComparer();
    a.Sort(c.Compare);
    CollectionAssert.AreEqual(expectedSorted, a);
}

Comparer:

public sealed class EOComparer : IComparer<string>
{
    public int Compare(string x, string y)
    {
        if (IsOthers(x)) return 1;
        if (IsOthers(y)) return -1;
        return string.Compare(x, y, StringComparison.Ordinal);
    }

    private static bool IsOthers(string str)
    {
        return string.Compare(str, "others", StringComparison.OrdinalIgnoreCase) == 0;
    }
}

Note how my use of string.Compare avoids all == null checks. And how StringComparison.OrdinalIgnoreCase avoids .ToLower() and thus avoids creating copies of the strings.

like image 167
weston Avatar answered Oct 24 '22 12:10

weston