簡體   English   中英

如何對文件txt行5000000進行排序?

[英]How can I sort the file txt line 5000000?

我有一個500000行的無序文件,其信​​息和日期如下:

for instance          desired Result
------------          ---------------      
 723,80                1,4   
 14,50                 1,5 
 723,2                 10,8
 1,5                   14,50 
 10,8                  723,2 
 1,4                   723,80       

現在我該如何實現這樣的事情呢?

我已經嘗試過sortedList和sorteddictionary方法但是沒有辦法在列表中實現新值,因為列表中有一些重復值。 如果你建議最好的方法,我會很感激。

還有一件事,我已經看到了這個問題,但是當我使用File時,這個問題使用了這個類!

C#List <>按x然后y排序

var result = File.ReadAllLines("...filepath...")
                 .Select(line => line.Split(','))
                 .Select(parts => new
                 {
                     V1 = int.Parse(parts[0]),
                     V2 = int.Parse(parts[1])
                 })
                 .OrderBy(v => v.V1)
                 .ThenBy(v => v.V2)
                 .ToList();

默認情況下,將正確處理重復項。 如果你想刪除它們,添加.Distinct()之后的某個地方,例如ReadAllLines

您需要將文件解析為由類定義的對象。 一旦它在對象中,您就可以開始對其進行排序。

public class myObject
{
    public int x { get; set; }
    public int y { get; set; }
}

現在,一旦將文件解析為對象列表,您應該可以執行以下操作:

var myList = new List<myObject>(); //obviously, you should have parsed the file into the list.
var sortedList = myList.OrderBy(l => l.x).ThenBy(l => l.y).ToList();

首先,對每一行進行排序,使它們處於正確的順序(例如[723,80] - > [80,723]

然后使用這樣的比較對所有行進行排序:

int Compare(Tuple<int,int> lhs, Tuple<int,int> rhs)
{
  int res = lhs.Item1.CompareTo(rhs.Item1)
  if(res == 0) res=lhs.Item2.CompareTo(rhs.Item2);

  return res;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM