Author Topic: UITable sorting  (Read 4419 times)

Jason

  • Guest
UITable sorting
« on: July 29, 2012, 04:03:52 PM »
hi,

One of my UITable was sorting alphabetically although I left the "sorted" option unchecked. I was able to overcome it by giving them the same name. I saw nothing wrong in the code.

Just wondering...

Jason

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITable sorting
« Reply #1 on: July 29, 2012, 07:32:44 PM »
line 65 of UITable:
  1. if (sorted) mChildren.Sort(SortByName);
That's the only place where they get sorted.

cjke.7777

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: UITable sorting
« Reply #2 on: July 03, 2013, 06:23:26 AM »
I know I'm new (first post!) && this is an old post but... I arrived to this thread when trying to find the answer to the same problem, so I figured it best that I help others.

The reason this is happening (and it happens only when sorted is set to false) is in fact the lines above, but not at the fault of NGUI mind you.

These lines:
  1. Transform myTrans = transform;
  2. mChildren.Clear();
  3.  
  4. for (int i = 0; i < myTrans.childCount; ++i)
  5. {
  6.     Transform child = myTrans.GetChild(i);
  7.  
  8.     if (child && child.gameObject && (!hideInactive || NGUITools.GetActive(child.gameObject))) mChildren.Add(child);
  9. }
  10.  

It is well documented (by not being documented at all!) that there is no guarantee when iterating over a transform the order of the children it returns - despite what you see in the inspector panel (which is always alphabetical).

Google 'unity3d transform children order' and you will see that the order is not guaranteed, and in fact GetChild is no longer part of the documentation as Transform interfaces from IEnumerable and so should be foreach'ed instead. Even when using a foreach though, you have no guarantee of order (when not using NGUI's sorted flag).

Two suggestions - either preface your children's names with a consistent label (such as "btn_") or implement your own sorting algorithm if alphabetical is not what you want.