简体   繁体   English

我怎样才能 output 值与每个列名对齐?

[英]How can I output the values aligned to each column name?

using System;
using System.Collections.Generic;
namespace freewritings
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<int> numlist = new List<int>() { 35, 30, 10, 5, 15,25, 20, 40 };

            Console.Write("Original List\t");
            Console.Write("Sorted List\t");

            foreach(var num in numlist)
            {
                Console.WriteLine(num);
            }
        }
    }
}

Expected Output:预期 Output:

Original List    Sorted List
35               5
30               10
10               15
5                20
15               25 
25               30
20               35
40               40

You can use Linq OrderBy() .您可以使用 Linq OrderBy() eg例如

List<int> numlist = new List<int>() { 35, 30, 10, 5, 15, 25, 20, 40 };
var orderedList = numlist.OrderBy(x => x).ToList();
orderedList.ForEach(x => Console.WriteLine(x));

A good ol'e fashioned for loop is a good option here一个好的老式for循环在这里是一个不错的选择

var numlist = new List<int>() { 35, 30, 10, 5, 15, 25, 20, 40 };
var sortedlist = numlist.OrderBy(n => n).ToList();

Console.Write("Original List\t");
Console.WriteLine("Sorted List");

for (int i = 0; i < numlist.Count; i++)
{
    Console.WriteLine($"{numlist[i]}\t{sortedlist[i]}");
}

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

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