Author Topic: Request: Sort UITable using Alphanumeric Sorting (algorithm attached)  (Read 4386 times)

helmesjo

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 116
    • View Profile
First, thanks for an amazing asset :)

Now, we where implementing a wide number of UITables in our project but somewhere along the line we noticed that the order of items where not as expected (all with "Sorted" enabled). After a lot of digging it just turned out to be the string.Compare you are using, which will not sort in a "human logical order", instead it will sort by ASCII-order (obviously! ><). Since this is rearly what people expect (we where naming our objects in the tables by index), I believe it would be nice to see a "real" sorter.

I found the following dude who (though back in 1997) did his own Alphanum Algorithm (License LGPL = free to use!) which now have been translated into multiple languanges (including C#)
http://www.davekoelle.com/alphanum.html

So, without starting to talk about areas I'm not an expert in, I did however implement his comparer and it worked just as expected (will even sort mix, ex: a1, a12, a2 etc., the same way you see stuff in the file-explorer).

So, could this be something to expect in a future update? It takes 10min tops! ;) (so I don't have to keep modifying your script :D)

Thanks!

Edit.
Another one, "well optimized" (not tested, since speed isn't an issue for us)
http://www.dotnetperls.com/alphanumeric-sorting

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Request: Sort UITable using Alphanumeric Sorting (algorithm attached)
« Reply #1 on: February 07, 2013, 10:09:59 AM »
You could simply name your objects 001, 002, 003 etc:
  1. int.ToString("N4").Replace(' ', '0');

helmesjo

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 116
    • View Profile
Re: Request: Sort UITable using Alphanumeric Sorting (algorithm attached)
« Reply #2 on: February 07, 2013, 11:51:37 AM »
You could simply name your objects 001, 002, 003 etc:
  1. int.ToString("N4").Replace(' ', '0');

First of all,
  1. int.ToString("N4").Replace(' ', '0');
gives me: 1.0000, 2.0000, 3.0000 etc. (really don't know any of the standard formats)

Second: Sure, 0008, 0009, 0010 (notice nr of zeros) will sort correctly, but I get a feeling this only "masks" the problem for the moment and is a lot uglier to type everytime in every table (when filled dynamically) than just
  1. label.text = i.ToString();

Again, sure I can get around it, just think it would be nice to see in a future update! :)

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Request: Sort UITable using Alphanumeric Sorting (algorithm attached)
« Reply #3 on: February 07, 2013, 06:13:02 PM »
I've been naming them with
  1. string.Format("{0:0000}, numberOfItem);

Then it sorts properly. I even think it sorts properly in Unity4 without the prefixed zeroes.