订单列表按字母顺序排列,但某些元素始终位于顶部

时间:2016-05-10 07:56:36

标签: c# asp.net-mvc entity-framework

class Employee   
{
    static void Main(string[] args)
    {
        List<favorite> f = new List<favorite>();

        f.Add(new favorite {id = 1, title ="MEN" });
        f.Add(new favorite { id = 2, title = "WOMEN" });
        f.Add(new favorite { id = 3, title = "BOYS" });
        f.Add(new favorite { id = 4, title = "GIRLS" });
        f.Add(new favorite { id = 5, title = "Ajay" });
        f.Add(new favorite { id = 6, title = "vijay" });
        f.Add(new favorite { id = 7, title = "Jitu" });
        f.Add(new favorite { id = 8, title = "Suresh" });
        f.Add(new favorite { id = 9, title = "Ramesh" });
        f.Add(new favorite { id = 10, title = "Akshay" })

        foreach (var item in f.OrderBy(e=>e.title).ThenBy(e=>e.title))
        {
            Console.WriteLine(item.title);
        }
        Console.ReadLine();
    }

    class favorite
    {
        public int id { get; set; }
        public string title { get; set; }
    }
}

以上列表包含4个默认收藏夹(MENWOWENBOYSGIRLS),我希望它们始终位于顶部,所有其他收藏夹位于字母顺序。

示例:我希望输出像

MEN
WOMEN
BOYS
GIRLS
AJAY
AKSHAY
JITU
LIKE
and so on ...

我想使用Entity Framework Lambda表达式执行此查询 我尝试使用OrderByThenBy执行此操作,但尚未找到合适的解决方案。

4 个答案:

答案 0 :(得分:5)

如果您不想更改favorite类,可以在某处定义这样的函数,

private static int GetPrimaryOrder(string title)
    {
        switch (title)
        {
            case "MEN":
                return 1;
            case "WOMEN":
                return 2;
            case "BOYS":
                return 3;
            case "GIRLS":
                return 4;
            default:
                return 5;
        }
    }

并将其用于初始排序,例如,

foreach (var item in f.OrderBy(x => GetPrimaryOrder(x.title)).ThenBy(e => e.title))
{
     Console.WriteLine(item.title);
}

答案 1 :(得分:1)

如果您想保持灵活性,您应该扩展您的类并添加另一个属性以指示排序/显示顺序。然后你可以对此进行排序或过滤。

class favorite
{
    public favorite(){sortOrder = int.MaxValue;} // default constructor, set the sort order to highest value possible. Alternatively you can make this a nullable property with default null value.
    public int id { get; set; }
    public int sortOrder { get; set; } // default high, sorted on low to high
    public string title { get; set; }
}

// code in main method
f.Add( new favorite {id = 1, title ="MEN", sortOrder=1 });
f.Add(new favorite { id = 2, title = "WOMEN", sortOrder=2 });
f.Add(new favorite { id = 3, title = "BOYS", sortOrder=3 });
f.Add(new favorite { id = 4, title = "GIRLS", sortOrder=4 });
// rest of your code, note that boolean defaults to false if you do not set it so the rest of your instances do not require change

// Sort by the sort order first and then title. Sort order is small first to large last, if they are the same then they are sorted by title
foreach (var item in f.OrderBy(e=>e.sortOrder).ThenBy(e=>e.title))
{
   Console.WriteLine(item.title);
}

或者你可以对默认类的id进行硬编码,并且基本上创建两个列表,在每个列表被排序后合并在一起,但它不是非常灵活的IMO。

答案 2 :(得分:0)

我认为您应该添加新的Order属性,然后设置您喜欢的

答案 3 :(得分:0)

最好的方法是添加额外的字段。

如果您只想使用链接解决它,您可以通过检查大写来订购。我认为顶部的都是大写的,这是你可以订购的东西。

     static void Main(string[] args)
    {
        List<favorite> f = new List<favorite>();

        f.Add(new favorite { id = 1, title = "MEN" });
        f.Add(new favorite { id = 2, title = "WOMEN" });
        f.Add(new favorite { id = 3, title = "BOYS" });
        f.Add(new favorite { id = 4, title = "GIRLS" });
        f.Add(new favorite { id = 5, title = "Ajay" });
        f.Add(new favorite { id = 6, title = "vijay" });
        f.Add(new favorite { id = 7, title = "Jitu" });
        f.Add(new favorite { id = 8, title = "Suresh" });
        f.Add(new favorite { id = 9, title = "Ramesh" });
        f.Add(new favorite { id = 10, title = "Akshay" });

        var list = f.OrderByDescending(e => IsAllUpper(e.title)).ThenBy(e => e.title).ToList();
        foreach (var item in list)
        {
            Console.WriteLine(item.title);
        }
        Console.ReadLine();
    }


    class favorite
    {
        public int id { get; set; }
        public string title { get; set; }
    }

    static bool IsAllUpper(string input)
    {
        for (int i = 0; i < input.Length; i++)
        {
            if (Char.IsLetter(input[i]) && !Char.IsUpper(input[i]))
                return false;
        }
        return true;
    }