C#使用冒泡排序对2个类别进行排序

时间:2012-11-27 19:39:47

标签: c#

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列,所以我所做的是按行读取文件,然后创建一个数组,用于存储用逗号分隔的字符串中的每一段数据。我现在要做的是先按幅度排序数据,然后使用bubblesort按深度排序。

1 个答案:

答案 0 :(得分:0)

如果bubblesort不是要求的一部分,你需要的只是一个排序列表,那么你可以使用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);
    }
}