简体   繁体   English

取得控制台输入,并从最高到最低排序

[英]Take console input and sort it from highest to lowest

As the title say, I want to take inputs from the user via the console and use a list or array to sort them, then output the list. 如标题所述,我想通过控制台从用户那里获取输入,并使用列表或数组对它们进行排序,然后输出列表。

Basically, the list is to have three variables, one string and two integers 基本上,列表包含三个变量,一个字符串和两个整数

(example: infexis, 20, 19 john, 24, 3 jane, 17, 28 (例如:infexis,20、19约翰,24、3简,17、28

and the list should be output in the same format, though just sorted based on the first number, so john would be first, then infexis, then jane. 并且列表应该以相同的格式输出,尽管只是根据第一个数字进行排序,因此john将是第一个,然后是infexis,然后是jane。 the name and second number doesnt matter in the sorting.) 名称和第二个数字在排序中无关紧要。)

I'm really new to c#, and I'm wondering how to take user input and add it to a list with no specified amount of variables, (so the list can take 2 entries, or 12, not just a specified number.) Any help would be appreciated! 我真的是c#的新手,我想知道如何接受用户输入并将其添加到没有指定数量的变量的列表中(因此列表可以包含2个条目,或者12个,而不仅仅是指定数量)。任何帮助,将不胜感激!

I highly suggest you introduction of class instead of string array in which you'll store user's input. 我强烈建议您引入类,而不是用于存储用户输入的字符串数组。

Example: 例:

public class Person 
{
  public string Name { get; set; }
  public int FirstNumber { get; set; }
  public int SecondNumber { get; set; }
}

public static void Main()
{
  //list of persons
  List<Person> people = new List<Person>();

  //Your piece of business logic ...

  //Creating person object

  var person = new Person();
  person.Name = Console.ReadLine();
  person.FirstNumber = Console.ReadLine();
  person.SecondNumber = Console.ReadLine();
  people.Add(person);

  //order by FirstNumber (needed to add: System.Linq namespace)
  var orderedPeople = people.OrderBy(p => p.FirstNumber);

  //output
  foreach(var p in orderedPeople)
  {
    Console.WriteLine(String.Format("{0} {1} {2}", p.Name, p.FirstNumber, p.SecondNumber));
  }
}

Note: I didn't test this code, so you can expect some compile time bugs, but you can get broader way how to handle your problem easier. 注意:我没有测试此代码,因此您可能会遇到一些编译时错误,但是您可以获得更广泛的方法来更轻松地处理问题。

Also, you can expand your class to meet additional requirement, like some other (optional) properties (ie user inputs) 另外,您可以扩展类以满足其他要求,例如其他一些(可选)属性(即用户输入)

I'd create a class which holds the type of information you describe. 我将创建一个类,其中包含您描述的信息类型。

class Person
{
    public string Name { get; set; }
    public List<int> Numbers { get; set; }
}

Then you're able to sort by the first element of Numbers . 然后,您可以按Numbers的第一个元素进行排序。

    static void Main(string[] args)
    {
        var people = new List<Person>()
        {
            new Person
            {
                Name = "Jeff",
                Numbers = new List<int>{ 1,2,3}
            },
            new Person
            {
                Name = "Sam",
                Numbers = new List<int> { 7 }
            }
        };

        foreach (var person in people.OrderByDescending(x => x.Numbers[0]))
        {
            Console.WriteLine(person.Name);
        }
    }

Output: Sam Jeff 输出:山姆·杰夫

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM