对List< string>进行排序根据另一个List< int> [英] Sort a List<string> according to another List<int>

查看:430
本文介绍了对List< string>进行排序根据另一个List< int>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个列表:

student = new list<string>() {"Bob" , "Alice" , "Roger" , "Oscar"};

Marks = new list<int>() {80,95,70,85};

我希望以最快的方式对学生进行标记排序,并且预期的输出必须是:

I want sort Student by Marks in fastest style and the expected output must be:

学生= {爱丽丝",奥斯卡",鲍勃",罗杰"}

Student = {"Alice","Oscar","Bob","Roger"}

在列表方法下是否有与list.sortlist.orderby相同的命令来达到目标​​?

Is there any command under list methods same as list.sort or list.orderby to achieve the goal?

推荐答案

不要使用2个数组.

您最好的方法是使用一个类来存储数据对.

Your best approach is to use a class to store pairs of data.

public class Student
{
  public string Name { get; set; }
  public int Mark { get; set; }
}

一旦您有一个学生对象数组

Once you have an array of Student objects

List<Student> students = new List<Student>();
students.Add(...);

然后您可以将名称与标记一起排序

Then you can sort the names together with the marks

var sortedStudents = students.OrderBy(s => s.Mark).ToList();

这篇关于对List&lt; string&gt;进行排序根据另一个List&lt; int&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆