- Home /
Sort List by another List Values - Different Type
Hello. I am trying to Sort a List to another List, which is of different type...
Explanation. I have 1 list with GameObjects and another List (Same Count), with the same GameObject's Names. The list with the names is reordered at some point. After that i want to sort the List with GameObjects to the List with the Names
Example
List<GameObject> myObjects = new List<GameObject>();
List<string> myObjectsNames = new List<string>();
void SomeFunction()
{
GameObject[] curSelection = Selection.gameObjects;
for (int i = 0; i < curSelection.Length; i++)
{
myObjects.Add(curSelection[i]); // Store the Selected GO to the list
myObjectsNames.Add(curSelection[i].name); // Store the Selected GO's names to the list
}
// At some point, the List "myObjectsNames" will be sorted in different way. All the time both Lists will have the same Count.
// After that... How can i sort myObjects List to be the same order as the myObjectsNames??
// I can use myObjects[#].name to Compare it to myObjectsNames, to get a match...
// I can do that with loop and 3rd List, but i was wondering if there is a better / faster way of doing it...
// EXAMPLE with Loop
List<GameObject> reorderedMyObjects = new List<GameObject>();
for (int a = 0; a < myObjectsNames.Count; a++) // Loop through Names
{
for (int z = 0; z < myObjects.Count; z++) // Loop through Objects
{
if (myObjectsNames[a] == myObjects[z].name) // Check if the names match
reorderedMyObjects.Add (myObjects[z]); // Add the matched object to the 3rd List.
}
}
// This loop works, but i have to assign the sorted gameObjects to third list. I want just to re-order the initial myObjects List
}
Based on the description of him re-organizing the list of names at elsewhere in his app. I'm guessing its more about the reordering of those names in some way ins$$anonymous$$d of just having a map of names to objects. Otherwise yes a dictionary would be the way to go.
Haven't tried, but i think i need a List exactly.
I've seen plenty of ways, for Reordering List. (By value, by number, etc), so i thought, that there might be a way of reordering List by matching the elements to another list.
$$anonymous$$aybe using System.Linq.. I'm not sure what exactly i can use...
Answer by HenryStrattonFW · Nov 09, 2015 at 03:14 PM
EDIT: Seems I missed in my skim reading the fact that your code already has this solution implemented. Sorry about that.
I would suggest creating a third list (during your re-sort) and then iterating over the names, each time finding the appropriate gameObject and popping it into your new list (thus resulting in a list of matching order).
Code removed, already exists in question post.
An alternative might be removing the splitting of the list at all. Is there any particular reason that you are storing the names in a separate list. Could you not just have the one list of game-objects, and then wherever you are re-ordering the names list, you instead re-order this list of game objects as you still would have access to their .name property. Or is there a specific reason you need the names in a list of their own?
I am using Reorderable List to show the names of the GameObjects. But if i use one list, it shows the names like "UnityEngine.Cube" and i can't edit that. That's why i need second list with just the names, so i can edit them, to show only Cube.
When i reorder the Reorderable List, it re-arranges the List with the names. Thus i need to sort the List with the Objects to the List with the names...
Haven't tried with Dictionary. That might be the way of doing it with one List (Dictionary), but i'm not sure if Reorderable List supports Dictionaries..