简体   繁体   中英

Best Way To Store and Sort Data in C#

I am building a program that will run 10000 iterations on a 6 sets of numbers. These numbers after being put through the iterations and calculated out and sorted in the following fashion.

*_Set1_****_Set2_****_Set3_****_Set4_****_Set5****_Set6_*
*********************************************************
**DESC******DESC******DESC******DESC******DESC*****DESC**
*********************************************************

Where Set(n) is the set of numbers and DESC is descending order of the numbers.

I have tried doing a LIST . But I am very unfamiliar with LIST and I'm not even sure it's what I'm looking for. I'll give you an example of what I was doing with the LIST

//Created a Output Class for Sim
 public class MC_OUT
    {

       public double output;
       public double OOIP;
       public double OGIP;
       public double EURO;
       public double EURG;
       public double rAREA;
       public double calcEURO;
       public double calcEURG;

        public MC_OUT(double output, double OOIP, double OGIP, double EURO, double EURG, double rAREA, double calcEURO, double calcEURG) { 
        this.output = output;
        this.OOIP = OOIP;
        this.OGIP = OGIP;
        this.EURO = EURO;
        this.EURG = EURG;
        this.rAREA = rAREA;
        this.calcEURO = calcEURO;
        this.calcEURG = calcEURG;


        }

//Declared list before Monte Carlo Sim.
List<MC_OUT> mcout = new List<MC_OUT>();


//Ran Code to Perform Calculations
//Then Executed this at End
 if (OOIP >= 0 && OGIP >= 0)
        {
            mcout.Add(new MC_OUT(NormSInv((i - .5) / iter), OOIP, OGIP, EURO, EURG, rAREA, EURO / (rAREA * rNET_H * rGCF) * 1000, EURG / (rAREA * rNET_H * rGCF) * 1000));
        }
//Then I got confused and couldn't figure out how to sort and then access the needed
 //information

So I guess after all this junk my main question would be

Thanks!

Now that you have created a list you can access the data by an index. For Example:

var firstRecord = mcout[0];
var firstEURO = firstRecord.EURO;

Or:

var firstEURO = mcout[0].EURO

If you want to sort the data it can be as simple as this.

var sortedData = mcout.Orderby(x => x.OOIP);

The above will sort the data based on one of the current properties you have. If you need to sort it differently you may need to think about adding another property to your object that would be better suited for sorting.

It is also easy to enumerate through the data with a foreach loop, or a for loop.

foreach (var mc in mcout)
{
    var euro = mc.EURO;
}

for (int i = 0; i < mcout.Count; i++)
{
    var euro = mcout[i].EURO;
}

Collections are something very widely used, and there are numerous types!! You will want to get to know them, and how to use them. If you google C# collections you will get a lot of information/tutorials. Here's an MSDN tutorial on collections . I could keep going on about how to access and use them, but there is really so much that you will need to dive in and start learning it through the information on the web. Hopefully the above examples, and links I have given you can get you started! Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM