I was wondering if it was possible to classify my dictionary by int value.
I would like to sort my key collection (which are music titles) in descending order of their respective value (int).
I have already created a list of music with their vote but I will like classified by this method in Update ():
Dictionary<string, int> voteCount;
....
foreach (string title in voteCount.Keys)
{
var item = GameObject.Find(title);
item.GetComponentInChildren<Text>().text = title + " : +" + voteCount[title].ToString();
//Ranking with Keys ? And edited Component.text by descending order
}
For example graphically, I will have my list of object (key) + their value (int value):
(michael jackson beat it) + 5
(Beyoncé - Halo) + 3
(Stromae - Papaoutai) + 2
(Sia - Never Give Up) + 2
…
Since the keys are placed in buckets based on their hashcode. The placement is fully dependent on the hashcode. No sorting possible.
If you want a sortable dictionary, see SortedDictionary:
Do note that the insertion/retrieval speed is slower than Dictionary. Dictionary has an O(1) insertion/retrieval, where as SortedDictionary has a O(log n) insertion/retrieval. Though that is faster than SortedList’s O(n) insertion. (with the exception of pre-sorted data):
I changed everything in SortedDictionary which seems to be a best solution.
But, I don’t see how I could specify the classification of my elements (according to the methods that I could read)…
When ever it does an insertion it’ll use this to compare 2 objects to then select where it’ll be inserted.
Now what you’ll need to do is create a field that you use to compare by. So whatever type you use as a key should probably have this field defined on itself.
If you do this… do not use a SortedDict. You basically subverted the entire point of a SortedDict by instead sorting it every time you access it.
Which is also an option.
But don’t use the 2 in conjunction… since you take the cost of a SortedDict (the fact it has slow insertions/retrieves), but ignored its benefits (the sorted nature).
You can use this very same linq code on a regular Dictionary.
Not what things you’re attempting to use to get it done. But your actual end goal. Why do you need this sorted? What are these GameObject’s you’re creating?
First things first, you can just upload images to the forums here and post them directly in. So that way you don’t have to link out to other sites. Or if you do link out… you can still embed them. Like so:
If so, then it’s just the child order that needs to be changed.
So you can do something like this… completely foregoing the entire dictionary thing.
First create a simple data container for each entry:
public class MusicEntryData : MonoBehaviour
{
public int Score;
public string Name;
public Text text;
void OnEnable()
{
this.MoveToScreen();
}
public void MoveToScreen()
{
text.text = string.Format("{0} : +{1}", Name, Score);
}
}
Then if you have the elements container, you can get the data entries, and you can use SetSiblingIndex to reorder. Since VerticalLayoutGroup uses the sibling index to order, this will update that:
Transform container = *the container holding elements*;
var arr = container.GetComponentsInChildren<MusicEntryData>();
System.Array.Sort(arr, (a, b) => a.Score.CompareTo(b.Score)); //you can flip a and b to sort the opposite direction
for(int i = 0; i < arr.Length; i++)
{
arr[i].transform.SetSiblingIndex(i);
}