首页 新闻 会员 周边

List Sort 多字段排序

0
悬赏园豆:15 [已关闭问题] 关闭于 2017-12-13 21:50
using System;
using System.Collections.Generic;
                    
public class Program
{
    public static void Main()
    {
        
        List<M> items=new List<M>();
        
        items.Add(new M{pro1="1003",pro2="B103",pro3="D103"});
        items.Add(new M{pro1="1002",pro2="B102",pro3="D102"});
        items.Add(new M{pro1="1005",pro2="B105",pro3="D105"});
        items.Add(new M{pro1="1001",pro2="B101",pro3="D101"});
        items.Add(new M{pro1="1004",pro2="B104",pro3="D104"});
        
        string queryText="1002";
        
        items.Sort((x,y)=>{
        
            return x.pro1.CompareTo(y.pro1);
           
        });
        
        foreach(var item in items){
            Console.Write(item.pro1+"\n");
        }
// output
// 1001
// 1002
// 1003
// 1004
// 1005 } }
public class M{ public string pro1{get;set;} public string pro2{get;set;} public string pro3{get;set;} }

如何能根据queryText变量的值进行排序?

如queryText="1002",那么输出为:

复制代码
        // output
        // 1002
        // 1001
        // 1003
        // 1004
        // 1005
复制代码

如果queryText在字段pro1中匹配,那么,此行记录位于最上面,反之,如果在pro1中都不能匹配,就从pro2中进行匹配。

就是优先从pro1,pro2中查找,如果此属性值与queryText相等,则此行记录往前排。

如queryText="B105";

       // output
        // 1005
        // 1001
        // 1003
        // 1003
        // 1004
Vivian软陶公仔的主页 Vivian软陶公仔 | 菜鸟二级 | 园豆:284
提问于:2017-12-11 21:24
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册