-1
using System;
using System.IO;

public class Earthquake
{
    public string Magnitude { get; set; }
    public string Location { get; set; }
    public string Latitude { get; set; }
    public string Longitude { get; set; }
    public string depth { get; set; }
    public string date { get; set; }
    public string EventID { get; set; }
    public string URL { get; set; }
    public Earthquake()
        : this(string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty)
    { }
    public Earthquake(string magna, string locate, string lat, string longi, string dept, string dat, string Event, string website)
    {
        Convert.ToDouble(Magnitude = magna);
        Location = locate;
        Convert.ToDouble(Latitude = lat);
        Convert.ToDouble(Longitude= longi);
        Convert.ToDouble(depth = dept);
        date = dat;
        EventID = Event;
        URL = website;
    }

}
public class ManageData
{   
    public void getData()
{
    string[] text = File.ReadAllLines(@"Earthquakes.csv");
    foreach (string line in text)
    {
        string[] myColumns = line.Split(',');
        Earthquake earth = new Earthquake(myColumns[0], myColumns[1], myColumns[2], myColumns[3], myColumns[4], myColumns[5], myColumns[6], myColumns[7]);
        //here i want to put each data in the Earthquake class
    }
}


}

本质上 csv 文件是 8 列,所以我所做的就是逐行读取文件,然后创建一个数组来存储以逗号分隔的字符串中的每条数据。我现在要做的是对数据进行排序,首先按幅度,然后使用冒泡排序按深度。

4

1 回答 1

0

如果冒泡排序不是要求的一部分,并且您只需要一个排序列表,那么您可以使用 LINQ。

像这样的东西应该工作。

public class ManageData
{
    public List<Earthquake> AllEarthquakes { get; private set; }
    public IOrderedEnumerable<Earthquake> getData()
    {
        string[] text = File.ReadAllLines(@"Earthquakes.csv");
        foreach (string line in text)
        {
            string[] myColumns = line.Split(',');
            Earthquake earth = new Earthquake(myColumns[0], myColumns[1], myColumns[2], myColumns[3], myColumns[4], myColumns[5], myColumns[6], myColumns[7]);
            //here i want to put each data in the Earthquake class
            AllEarthquakes.Add(earth);
        }

        return AllEarthquakes.OrderBy(eq => eq.Magnitude).ThenBy(eq => eq.depth);
    }
}
于 2012-11-27T19:48:03.513 回答