Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Sorting List of data

Discussion in 'Scripting' started by phoda, Jun 10, 2016.

  1. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    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

    Code (CSharp):
    1. List<string> cpuName = new List<string>();
    2.     List<int> cpuID = new List<int>();
    3.     List<int> cpuScore = new List<int>();
    4.     List<int> cpuActive = new List<int>();
    5.     List<int> cpuPrice = new List<int>();
    6.     List<int> cpuTech = new List<int>();
    7.     List<int> cpuReleaseYear = new List<int>();
    8.     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
     
  2. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    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.
     
    phoda likes this.
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
  4. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    you have any examples or something on how to do that, how to store class to a list? Like List<myClass> 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
     
  5. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    Example:
    Code (CSharp):
    1. public class CpuData
    2. {
    3.     public string Name;
    4.     public int ID;
    5.     public int Score;
    6.     public int Active;
    7.     public int Price;
    8.     public int Tech;
    9.     public int ReleaseYear;
    10.     public int ReleaseDay;
    11. }
    12.  
    13. List<CpuData> data = new List<CpuData>();
     
    phoda likes this.
  6. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384

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

    Code (CSharp):
    1. List.Add(CpuData);
    2.  
    3. then can i change or access id or name by
    4.  
    5. list[0].cpuScore = 5;
    ?
     
  7. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    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
    Code (CSharp):
    1. CpuData a = new CpuData() { ID = 232 };
    2. CpuData b = new CpuData() { ID = 775 };
    3.  
    4. Dictionary<int, CpuData> data = new Dictionary<int, CpuData>();
    5. data.Add(a.ID, a);
    6. data.Add(b.ID, b);
    7.  
    8. data[232].Score = 5;
    9. data[775].Score = 3;
    //Name version
    Code (CSharp):
    1. CpuData a = new CpuData() { Name = "CpuONE" };
    2. CpuData b = new CpuData() { Name = "CpuTWO" };
    3.  
    4. Dictionary<string, CpuData> data = new Dictionary<string, CpuData>();
    5. data.Add(a.Name, a);
    6. data.Add(b.Name, b);
    7.  
    8. data["CpuONE"].Score = 5;
    9. data["CpuTWO"].Score = 3;
    You could then create a new List<CpuData>(data.Values) and sort that list with linq or IComparable.
     
    Last edited: Jun 10, 2016
    phoda likes this.
  8. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384

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