- Home /
How to sort a List by a class paramater
1) I have a Character Class constructor formed as such:
public Character(string n, string e, int a, int b, int c, int d)
2) In another Class, I create 8 such characters and add them to a List. How can I order that List so that 'int b' is the one that decides which Character will be at the top? The List would be in descending order with the largest 'int b' at the top. I have read of some options with using Linq or IEnumerable. Even thought about a PriorityQueue. What would be the most efficient way to implement this?
Thank you
Answer by iwaldrop · Jun 19, 2013 at 06:22 AM
In C# you can use LINQ.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class ListTesdt : MonoBehaviour
{
[System.Serializable]
public class TestData
{
public int data;
}
public List<TestData> testData;
void Awake()
{
testData = testData.OrderBy(x => x.data).ToList();
foreach (TestData t in testData)
Debug.Log(t.data);
}
}
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to obtain a gameObject from an List with the name? 1 Answer
Sorting a List of Dictionaries in C#? 4 Answers
my random task generator not working 1 Answer
Sorting a list based of a variable 1 Answer