商店名称+编号,并根据编号按大小顺序排序 [英] Store name + number and sort in size order based on the numbers

查看:120
本文介绍了商店名称+编号,并根据编号按大小顺序排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

做一个大学项目,我有点卡住了.

Doing a college project and I'm a bit stuck..

基本上,我需要为员工姓名输入字符串,并为他们出售的物业数量输入整数.

Basically, I need to take the string input for an employee name and an integer input for the amount of properties they sold.

然后我需要根据售出的房屋数量按降序打印这些值,因此,例如,我需要打印如下内容:

I then need to print these values in descending order based on the number of properties sold, so, for example, I need to print something like this:

(加粗以使其易于阅读)

(Bolded to make it easier to read)

名称:约翰已售物业:5

名称:彼得已售物业:4

名称:Craig 售出的商品:3

Name: Craig Properties Sold: 3

我将出售的名称和属性存储在各自的单独列表中,我知道如何按大小顺序对数字列表进行排序,但是如何将其与适当的名称链接回去?

I have stored the name and properties sold in their own separate lists, I know how to sort the number list in size order but then how do I link it back with the appropriate name?

    List<string> names = new List<string>();
    List<int> numbers = new List<int>();

    for(int i = 0; i < 2; i++){

      Console.WriteLine("Please enter the employee name: ");
      names.Add(Console.ReadLine());

      Console.WriteLine("Please enter the number of Properties Sold: ");
      numbers.Add(int.Parse(Console.ReadLine()));        
    }

    numbers.Sort();
    numbers.Reverse();

    foreach(int i in numbers){

    Console.WriteLine(i);
    }

任何帮助都值得赞赏,我不是最好的编码员,如果我问一个愚蠢的问题,对不起.

Any help is appreciated, I am not the best coder so sorry if I'm asking a dumb question.

推荐答案

理想情况下,您将为此使用类,如以下示例所示.

Ideally you would be using a class for these, as seen in the example below.

 public class Owner
{
    public string Name { get; }
    public int PropertiesSold { get; }
    public Owner(string name, int propertiesSold)
    {
        Name = name;
        PropertiesSold = propertiesSold;
    }
}

这样,您可以同时跟踪名称和属性编号.您的循环变成这样:

This way you can keep track of the name and # of properties at the same time. Your loop turns into this:

 List<Owner> owners = new List<Owner>();

        for (int i = 0; i < 2; i++)
        {

            Console.WriteLine("Please enter the employee name: ");
            string name  = Console.ReadLine();

            Console.WriteLine("Please enter the number of Properties Sold: ");
            int numProperties = int.Parse(Console.ReadLine());
            owners.Add(new Owner(name, numProperties));
        }

您可以使用Linqs OrderByDescending(如此处所示)对所有者列表进行排序. LINQ Orderby降序查询.

And you can use Linqs OrderByDescending as seen here to sort the list of Owners. LINQ Orderby Descending Query.

这篇关于商店名称+编号,并根据编号按大小顺序排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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