简体   繁体   English

如何对文件txt行5000000进行排序?

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

i've got a disordered file with 500000 line which its information and date are like the following : 我有一个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       

Now how can i implement such a thing ? 现在我该如何实现这样的事情呢?

I've tried the sortedList and sorteddictionary methods but there is no way for implemeting a new value in the list because there are some repetative values in the list. 我已经尝试过sortedList和sorteddictionary方法但是没有办法在列表中实现新值,因为列表中有一些重复值。 I'd appreciate it if u suggest the best possible method . 如果你建议最好的方法,我会很感激。

One more thing , i've seen this question but this one uses the class while i go with File! 还有一件事,我已经看到了这个问题,但是当我使用File时,这个问题使用了这个类!

C# List<> Sort by x then y 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();

Duplicates will be handled properly by default. 默认情况下,将正确处理重复项。 If you want to remove them, add .Distinct() somewhere, for example after ReadAllLines . 如果你想删除它们,添加.Distinct()之后的某个地方,例如ReadAllLines

You need to parse the file into an object defined by a class. 您需要将文件解析为由类定义的对象。 Once it's in the object, you can start to sort it. 一旦它在对象中,您就可以开始对其进行排序。

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

Now once you get the file parsed into a list of objects, you should be able to do something like the following: 现在,一旦将文件解析为对象列表,您应该可以执行以下操作:

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

First, sort each row so that they are in the correct order (eg [723,80] - > [80,723] 首先,对每一行进行排序,使它们处于正确的顺序(例如[723,80] - > [80,723]

Then sort all rows using a comparison something like this: 然后使用这样的比较对所有行进行排序:

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