为了账号安全,请及时绑定邮箱和手机立即绑定

按两个不同的列表对列表进行排序

按两个不同的列表对列表进行排序

C#
www说 2022-09-04 16:55:28
我正在尝试按类别对一些数据进行排序。每个类别都有一个计数,或类别中的条目数量和我给该类别的分数。分数是最重要的,在排序时应始终首先考虑。计数不如分数重要,但仍然很重要,在排序时应将其视为第二位。List<string> categories = new List<string>();List<int> count = new List<int>();List<int> score = new List<int>();categories.Add("category1");count.Add(23);score.Add(8);...for(int i = 0; i < sortedcategories.Count; i++) {    Console.WriteLine(sortedCategories[i] + sortedScore[i] + sortedCount[i]);}// Should Output/*category8 [Score: 10] [Count: 8]category2 [Score: 10] [Count: 5]category1 [Score: 8] [Count: 23]category5 [Score: 7] [Count: 12]category4 [Score: 5] [Count: 28]category3 [Score: 5] [Count: 25]category7 [Score: 5] [Count: 17]category6 [Score: 2] [Count: 34]*/如何执行排序操作,为我提供上述输出?(如果这更容易与数组,我也可以使用数组)
查看完整描述

2 回答

?
白衣非少年

TA贡献1155条经验 获得超0个赞

不建议使用 3 个独立列表。创建类以存储类别


public class Category 

{

     public string Name { get; set; }

     public int Score { get; set; }

     public int Count { get; set; }

}

然后用类别填充列表


// In your method add a category to list

var categories = new List<Category>();

categories.Add(new Category {

     Name = "Category1",

     Score = 10,

     Count = 3

});

使用 System.Linq 对类别进行排序


var sortedCategores = categories.OrderByDescending(x => x.Score).ThenByDescending(x => x.Count).ToList();

循环访问集合


foreach(var category in sortedCategores)

{

    Console.WriteLine($"{category.Name} [Score: {category.Score}] [Count: {category.Count}]");

}


查看完整回答
反对 回复 2022-09-04
?
有只小跳蛙

TA贡献1824条经验 获得超8个赞

可能最简单的方法是创建一个类来保存每个列表中的关联属性,而不是尝试管理一堆列表及其项的顺序。我们还可以重写此类的 ToString 方法,以便它输出您当前默认使用的格式化字符串:


class Category

{

    public string Name { get; set; }

    public int Count { get; set; }

    public int Score { get; set; }


    public override string ToString()

    {

        return $"{Name} [Score: {Score}] [Count: {Count}]";

    }

}

然后,您可以创建此类型的单个列表,而不是三个不同的列表。下面是一个示例,它使用现有列表来填充新列表,但理想情况下,您可以修改向三个列表添加项的代码,而不是向单个列表添加新项。拥有此单个列表后,可以使用扩展方法 OrderBy(将最小的项目放在前面)或 OrderByDescending(将最大的项目放在第 一位)按您喜欢的任何属性(然后按任何其他属性)对其进行排序:CategorySystem.Linq


var items = new List<Category>();


// Create a list of items based off your three lists

// (this assumes that all three lists have the same count).

// Ideally, the list of Items would be built instead of the three other lists

for (int i = 0; i < categories.Count; i++)

{

    items.Add(new Category

    {

        Name = categories[i],

        Count = count[i],

        Score = score[i]

    });

}


// Now you can sort by any property, and then by any other property

// OrderBy will put smallest first, OrderByDescending will put largest first

items = items.OrderByDescending(item => item.Score)

    .ThenByDescending(item => item.Count)

    .ToList();


// Write each item to the console

items.ForEach(Console.WriteLine);


GetKeyFromUser("\nDone! Press any key to exit...");

输出

//img1.sycdn.imooc.com//631468d00001a63204520241.jpg

查看完整回答
反对 回复 2022-09-04
  • 2 回答
  • 0 关注
  • 128 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号