Getting objects out of a list and then comparing them
Hello!
I have a List which i would like to sort. Basically the list cotains of GameObjects which have a text component on the 2nd index of the child sitting which i can use to sort it. I basically have 2 Objects one with a time at: 10am and the other one at 6am as an example. I want to get the 6am one in front of the 10am one which i would do via bubble sort.
My Code:
private void SortList(List<GameObject> evntsList){ //BubbleSort
GameObject temp;
for (int write = 0; write < eventsList.Count; write++) {
for (int sort = 0; sort < eventsList.Count - 1; sort++) {
GameObject temp2 = eventsList [sort].transform.GetChild (2).gameObject;
GameObject temp3 = eventsList [sort+1].transform.GetChild (2).gameObject;
Text tempT1 = temp2.GetComponent<Text>();
Text tempT2 = temp3.GetComponent<Text>();
Debug.Log(temp2.GetComponent<Text>().text);
Debug.Log (temp3.GetComponent<Text>().text);
string s1 = tempT1.text;
string s2 = tempT2.text;
print (s1+"S1");
print (s2+"S2");
int time1;
int time2;
time1 = int.Parse(s1.Substring(0,2));
time2 = int.Parse(s2.Substring(0,2));
print (time1+"<Time1 Time2>"+ time2);
if (time1 > time2) {
temp = eventsList[sort + 1];
eventsList[sort + 1] = eventsList[sort];
eventsList[sort] = temp;
}
}
}
}
My Problem: No matter what i seem to do, both objects are the same (temp2 and temp3) I found out by printing the values of the text. In our example with 10am and 6am both values i print (doesnt matter which print in the code) they always spit out 6. even tho time1 should be 10 and time2 should be 6 which would then make the if(time1 > time2) statement true and start the sorting.
No matter what i seem to do it just doesnt work. What could i do here? Did i do something wrong?
Your answer
Follow this Question
Related Questions
Error With Blocks = Block1.Intersect(Block2); error CS0266: 1 Answer
Using a specific function from a generic object. 0 Answers
How do you Sort List by field? 1 Answer
How to delete an object if there is another object of the same type at that position ? 1 Answer
All GameObjects list to a GameObject? 0 Answers