Author Topic: Custom UITable Sorting not working on device  (Read 3274 times)

DuckOfDoom

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 10
    • View Profile
Custom UITable Sorting not working on device
« on: May 08, 2013, 04:38:12 AM »
I had to implement sorting items by their price for one of my UITables. I named all of GameObjects in that table according to their price(like 5100), but because you can't compare strings as integers(2111 is bigger than 11111 string-style), I had to implement a comparison of strings as integers using int.TryParse. Here is the example methods(in class MiscTools, doesnt matter):

  1. public static int CompareTransformsByNames(Transform first, Transform second)
  2. {
  3.         return GetIntFromString(first.name).CompareTo(GetIntFromString(second.name));
  4. }
  5.  
  6. public static int GetIntFromString(string number)
  7. {
  8.         int output;
  9.  
  10.         if (!int.TryParse(number, out output))
  11.         {
  12.                 Debug.Log(string.Format("Unsupported string formant in sorting: {0}", number));
  13.                 return 0;
  14.         }
  15.                 return output;
  16. }

And this is how it is used:
  1. myTable.children.Sort(MiscTools.CompareTransformsByNames);
  2. myTable.Reposition();
  3.  

I also tried LINQ approach:
  1. myTable.children.OrderBy(x => MiscTools.GetIntFromString(x.name));
  2. myTable.Reposition();
  3.  

Both variants work like a charm in editor, table is sorted right, but on devices(both iOS and Android) it does no sorting at all, without any exceptions or anything. I know the only place the table gets sorted is line 66 of UITable.cs(
  1. if (sorted) mChildren.Sort(SortByName);
) but "sorted" flag is off on all tables.

What could possible be a reason of such strange behaviour on devices specifically?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Custom UITable Sorting not working on device
« Reply #1 on: May 08, 2013, 05:48:13 PM »
00010
00100
01234

If you append zeroes in front, everything works as it should.

DuckOfDoom

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 10
    • View Profile
Re: Custom UITable Sorting not working on device
« Reply #2 on: May 13, 2013, 04:37:02 AM »
Thanks, ill try that. I'm just curious about why the behaviour differs in editor and on device.