Author Topic: OnCustomSort example?  (Read 6252 times)

capitalj

  • Jr. Member
  • **
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 88
    • View Profile
OnCustomSort example?
« on: May 10, 2014, 10:48:14 PM »
Hi, I'd like to use the OnCustomSort delegate of UIGrid, but I'm not sure how it works. Are there any examples of this?

I'd like to sort using an int value.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: OnCustomSort example?
« Reply #1 on: May 11, 2014, 12:42:56 AM »
Where would this 'int' come from? You get 2 transforms in your sort function. Just look at UIGrid.SortByName, for example. You can set the custom callback via grid.onCustomSort = YourSortFunc;, but your "YourSortFunc" still has to accept 2 transforms as the parameters.

capitalj

  • Jr. Member
  • **
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 88
    • View Profile
Re: OnCustomSort example?
« Reply #2 on: May 12, 2014, 08:23:22 PM »
The int is just on a script on the object that is to be sorted. What are the 2 transforms that the function has to accept?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: OnCustomSort example?
« Reply #3 on: May 13, 2014, 08:31:23 AM »
Grid has children which are transforms from the grid's POV. In your case GetComponent<YourType> on each of the transforms, and compare the int value you desire.

capitalj

  • Jr. Member
  • **
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 88
    • View Profile
Re: OnCustomSort example?
« Reply #4 on: May 13, 2014, 09:41:36 PM »
Sorry, I meant is there an example of how to use this functionality? I'm not familiar with how to structure this kind of function, so I don't even know where to start with creating the custom sort.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: OnCustomSort example?
« Reply #5 on: May 14, 2014, 06:20:16 AM »
As I mentioned just look at UIGrid.SortByName -- it's just your basic sorting function, standard C# functionality. It's a register-able sorting function. Your function would look like:
  1. static public int MyCustomSort (Transform a, Transform b)
  2. {
  3.     MyCustomComponent c1 = a.GetComponent<MyCustomComponent>();
  4.     MyCustomComponent c2 = a.GetComponent<MyCustomComponent>();
  5.     return int.Compare(c1.someValue, c2.someValue);
  6. }

capitalj

  • Jr. Member
  • **
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 88
    • View Profile
Re: OnCustomSort example?
« Reply #6 on: May 14, 2014, 12:47:42 PM »
Ok thanks for the example, that makes sense.

kkman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: OnCustomSort example?
« Reply #7 on: December 16, 2015, 02:27:28 AM »
Hi there,

if I want to sort the gameobject's name which is a date format i.e (11/05/2015). how can i achieve this?

I hope someone can give me a hint. very much appreciated.

kkman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: OnCustomSort example?
« Reply #8 on: December 16, 2015, 02:56:50 AM »
I work it out.

thanks