Author Topic: BUG: UISpriteAnimation sprite list not sorting correctly without leading zeros  (Read 2020 times)

Buey

  • Guest
Image_1
Image_2
Image_3
...
Image_20

will be ordered as

Image_1
Image_10
Image_11

etc. since you're using string sort.

You can use a sort comparator like

  1. int SpriteListSort(string a, string b)
  2. {
  3.         int iA, iB;
  4.  
  5.         if (int.TryParse(a.Replace(mPrefix, ""), out iA) &&
  6.             int.TryParse(b.Replace(mPrefix, ""), out iB))
  7.             return iA - iB;
  8.  
  9.         return a.CompareTo(b);
  10. }
  11.  
  12. mSpriteNames.Sort(SpriteListSort);
  13.  

but this assumes that mPrefix is exact (defaults to string sort otherwise).

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
That's how string sorting works. Just add zeroes and it'll be fine.