I need help on this one. I took some research about sorting GameObject in alphabetical and numeric order. I implemented this code like this:
if(SortCardOrder) {
List<GameObject> cardOrder = null;
cardOrder = this.card[0].OrderBy(go=>go.name).ToList(); // --> I use this code to sort by name.
foreach(GameObject card in cardOrder) {
CardPosition[] position = card.GetComponents<CardPosition>();
// Display result here.
print ("[ CARD IN HAND ] --> " + position[0].getSuit() + " " + position[0].getCardNumber() +
" at player " + 1 + ".");
cardText.text += "" + position[0].getSuit() + " " + position[0].getCardNumber() +
" at player " + 1 + ".
";
}
}
When I check it via console or via GUI Text preview and I found that it was sorted not completely alphabetical and numerical but lexicographical. (See example below.)
/**
*
* EXPECTED, RESULT IN OUTPUT: (Correct)
* Club 1
* Club 2
* Club 10
* Diamond 5
* Diamond 6
* Heart 10
* Spade 1
* Spade 2
* Spade 10
*
* REALITY, RESULT IN OUTPUT: (Wrong)
* Club 1
* Club 10
* Club 2
* Diamond 5
* Diamond 6
* Heart 10
* Spade 1
* Spade 10
* Spade 2
*
* (Sometimes, it display the result by sorting the one with numbers in lexical order
* first before the letters.)
*
* Ex. "Hearts 1, Clubs 3, Spade 3, Diamonds 4, Diamonds 5"
*
*/
I found this source useful and the only way to sort game object by name is to sort alphabetically and numerically in order to met the result for my card game so that I can use it to compute for the end game results. Currently, I still looking for this answer for this problem about this sorting problem. A contribution for help or tip will be appreciated.
UPDATE (As of 8/6/2015):
After I observed their answers about IComparer
syntaxes, I check on this link and I made this solution (with Comparison<GameObject>
as the parameter of the Sort()
method) like this:
cardOrder = this.card[playerIndex];
cardOrder.Sort((s1, s2)=>{
string pattern = "([A-Za-z])([0-9]+)";
string h1 = Regex.Match(s1.name, pattern).Groups[1].Value;
string h2 = Regex.Match(s2.name, pattern).Groups[1].Value;
if (h1 != h2) return h1.CompareTo(h2);
string t1 = Regex.Match(s1.name, pattern).Groups[2].Value;
string t2 = Regex.Match(s2.name, pattern).Groups[2].Value;
return int.Parse(t1).CompareTo(int.Parse(t2));
});
Unfortunately, I still got errors yet. This error message shown in the console like this:
FormatException: Input string was not in the correct format
System.Int32.Parse (System.String s) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Int32.cs:629)
CardInHand.<ResortAndRegroupCard>m__0 (UnityEngine.GameObject s1, UnityEngine.GameObject s2) (at Assets/Bet Module/CardInHand.cs:594)
System.Array.qsort[GameObject] (UnityEngine.GameObject[] array, Int32 low0, Int32 high0, System.Comparison`1 comparison) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Array.cs:1772)
And, in order to re-arrange string value from a GameObject
name (since I created a List
object for storing a lot of GameObjects
) is to find out how to sort a List
of GameObjects
by fetching its name as string value and compare it in order to arrange in alpha-numeric order.