Sorting List of data

I need to make a list of data that can be sorted (you know like in task manager or some other data when you press at top name and sorts by names ascending or descending). I want to make that but dont know how its called or how to look it online because i dont know name.

So anyone at least have some tips?

I have data structured like this

List<string> cpuName = new List<string>();
    List<int> cpuID = new List<int>();
    List<int> cpuScore = new List<int>();
    List<int> cpuActive = new List<int>();
    List<int> cpuPrice = new List<int>();
    List<int> cpuTech = new List<int>();
    List<int> cpuReleaseYear = new List<int>();
    List<int> cpuReleaseDay = new List<int>();

so as you can see i made this so i can add cpus as time moves on, so how to make that kind of list to sort everything? any links or info would be great

thanks

I think you’d be better off setting up a class that stores all the information about a single cpu and then using a single list to store it all. You could then implement IComparable for the class with a switch based on which column to sort by along with the descending/ascending details. Then just call Sort() on your list when a different column is clicked.

1 Like

as pointed out, you could do IComparable. You might also consider linq
http://www.functionx.com/csharp/linq/Lesson03.htm
If you scroll down on that link it shows you how to do a linq sort on a class.

you have any examples or something on how to do that, how to store class to a list? Like List and then like list[0] stores whole class for one cpu?

can you say how that list is called so i know how to list it on screen

Example:

public class CpuData
{
    public string Name;
    public int ID;
    public int Score;
    public int Active;
    public int Price;
    public int Tech;
    public int ReleaseYear;
    public int ReleaseDay;
}

List<CpuData> data = new List<CpuData>();
1 Like

thing bugging me here is how i define score etc in that list like

List.Add(CpuData);

then can i change or access id or name by

list[0].cpuScore = 5;

?

I’m not sure why you would want to be able to update the values directly with what I assume is just a display list for a client. You should keep a reference to the data somewhere else so the values can be changed. Then when a value is updated trigger a callback to re-sort/rebuild the display list.

However, if you really want to be able to access the CpuData by name or ID you would need to store it in a dictionary.

//ID version

CpuData a = new CpuData() { ID = 232 };
CpuData b = new CpuData() { ID = 775 };

Dictionary<int, CpuData> data = new Dictionary<int, CpuData>();
data.Add(a.ID, a);
data.Add(b.ID, b);

data[232].Score = 5;
data[775].Score = 3;

//Name version

CpuData a = new CpuData() { Name = "CpuONE" };
CpuData b = new CpuData() { Name = "CpuTWO" };

Dictionary<string, CpuData> data = new Dictionary<string, CpuData>();
data.Add(a.Name, a);
data.Add(b.Name, b);

data["CpuONE"].Score = 5;
data["CpuTWO"].Score = 3;

You could then create a new List(data.Values) and sort that list with linq or IComparable.

1 Like

needed some time and unitys tutorials but i got it now, thanks