簡體   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);
            }
        }
    }
}

預期 Output:

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

您可以使用 Linq OrderBy() 例如

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));

一個好的老式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