[ 11 posts ] Go to page 1, 2 Next

Anthony R

  • Username: draftomatic
  • Joined: Fri Sep 17, 2010 2:01 pm
  • Posts: 15
  • Offline
  • Profile

Numbers being sorted like text

Post Posted: Sun Oct 03, 2010 10:48 am
+0-
I have some numbers in a column, although I guess they are actually put in the DT as text (I'm not using formatters). So, sorting isn't working, i.e. it's sorting my numbers like this:

0
0
156
111
2
25
67
99

Is there a "number" formatter? Where is the list of formatters? What's the best way to make it sort correctly?

I'm confused because the client-side sorting example uses text as numbers and it sorts correctly...

K o v a c s

  • Username: Kovacs
  • Joined: Wed Feb 11, 2009 8:55 am
  • Posts: 100
  • Location: The Biggest Little City in the World
  • Offline
  • Profile

Re: Numbers being sorted like text

Post Posted: Sun Oct 03, 2010 11:38 am
+0-
Carefully read this:

http://developer.yahoo.com/yui/datatable/#basicsort

Pay close attention to the second paragraph:

Quote:
Keep in mind that sorting a Column first sorts the data in the underlying RecordSet and then updates the DOM UI to reflect this new sort order. Therefore, the type of the data (e.g., String, Number, Date, etc.) held in the RecordSet determines the sort algorithm, not the type as defined in your Column definition formatter property.


And to the 4th paragraph--e.g.,
Quote:
Note that setting the sortedBy property does not perform a sort on the Column, it merely applies the appropriate CSS when the DataTable loads.


"Where is the list of formatters?" -- it's on the same page, here:

http://developer.yahoo.com/yui/datatable/#format

Just keep in mind that "sorting" and "formatting" are entirely different things.

Satyam

YUI Contributor

  • Username: Satyam
  • Joined: Tue Dec 09, 2008 12:34 am
  • Posts: 2016
  • Location: Sitges, Spain
  • GitHub: Satyam
  • Gists: Satyam
  • IRC: DevaSatyam
  • YUI Developer
  • Offline
  • Profile

Re: Numbers being sorted like text

Post Posted: Sun Oct 03, 2010 1:41 pm
+0-
You need to use a parser in the DataSource so that numbers coming as strings in whichever message format you are using, are converted to numbers. Then, the sort function will sort them as numbers so, for example, you would do:

myDataSource.responseSchema.fields = [ .....
{key:'someNumber',parser:'number'}
];

Anthony R

  • Username: draftomatic
  • Joined: Fri Sep 17, 2010 2:01 pm
  • Posts: 15
  • Offline
  • Profile

Re: Numbers being sorted like text

Post Posted: Tue Oct 05, 2010 10:04 am
+0-
Cool thanks fellas =)

Anthony R

  • Username: draftomatic
  • Joined: Fri Sep 17, 2010 2:01 pm
  • Posts: 15
  • Offline
  • Profile

Re: Numbers being sorted like text

Post Posted: Thu Aug 18, 2011 10:36 am
+0-
I find simple column sorting curiously difficult to pull off. One of my problems was having sortedBy in the datatable constructor, which was locking the sort order. I took that out, and then I could sort one of my text-formatted columns correctly.

However, have some number-formatter columns that I can't get sorted like numbers. I've tried all combinations of using number formatters and number parsers, with no luck. The confusing thing is that only my last column is sorting correctly, with formatter: "number" and parser: "number". Other columns with the same combo and same data aren't working. What's going on here?

Finally, I am trying to sort the table when the page loads by using:
Code:
dataTable.sortColumn(dataTable.getColumn("suName"), YAHOO.widget.DataTable.CLASS_ASC);


This sorts the column "suName," which has a text formatter and a string parser, but the sorting isn't correct or alphabetical.

Please help! Been having these problems for months.

Anthony R

  • Username: draftomatic
  • Joined: Fri Sep 17, 2010 2:01 pm
  • Posts: 15
  • Offline
  • Profile

Re: Numbers being sorted like text

Post Posted: Thu Aug 18, 2011 11:01 am
+0-
Okay, so I have fixed some of my number sorting problems by changing the datatype of what I use to add rows (my table is populated by calls to addRow()). Instead of passing strings I converted to ints manually (why wasn't the parser working???).

I still need to sort the "suName" column when the page loads. How can I do it?

Todd Smith

YUI Contributor

  • Username: stlsmiths
  • Joined: Thu Nov 05, 2009 10:03 am
  • Posts: 675
  • GitHub: stlsmiths
  • Gists: stlsmiths
  • IRC: t_smith
  • Offline
  • Profile

Re: Numbers being sorted like text

Post Posted: Thu Aug 18, 2011 11:21 am
+0-
Maybe you can provide a pastie link to your code and some of the data.

We can't help you fix what we can't see!

Anthony R

  • Username: draftomatic
  • Joined: Fri Sep 17, 2010 2:01 pm
  • Posts: 15
  • Offline
  • Profile

Re: Numbers being sorted like text

Post Posted: Thu Aug 18, 2011 11:54 am
+0-
stlsmiths wrote:
We can't help you fix what we can't see!


Yea I know... it's difficult for me to post code samples because this isn't actually a web application... Google SketchUp allows plugin developers to create DHTML dialogs in a desktop application, and the data is loaded into JS in a very unorthodox way... So basically any example I make will be a bastardized version of my actual code. But I will try.

Todd Smith

YUI Contributor

  • Username: stlsmiths
  • Joined: Thu Nov 05, 2009 10:03 am
  • Posts: 675
  • GitHub: stlsmiths
  • Gists: stlsmiths
  • IRC: t_smith
  • Offline
  • Profile

Re: Numbers being sorted like text

Post Posted: Thu Aug 18, 2011 12:17 pm
+1-
Okay.

If you read "KOVACS" response earlier in this thread it is right on target. Satyam was also exactly correct.

I'm not sure what framework you are using, but adding data to a DataTable via .addRows is not a very stable and correct approach. I would highly recommend you consider converting it to a via DataSource. Because within DataSource it does all of the conversion and parsing for you. Again, maybe the "framework" you are using won't permit that.

As for using the "sortedBy" configuration parameter, as the above posting says, this parameter only "looks" like the table is sorted, id doesn't actually "perform the sort". It is intended for applications where the DataSource is populated from, for example a SQL query that has an ORDER BY clause and the data arrives already sorted.

Assuming your DataTable is initially unsorted, you can use "sortedBy" to change the initial appearance properly, but you still need to do a .sortColumn after the DT renders to actually conduct the sort. In your case, if you are using .addRow to populate the table, I would recommend you do your .sortColumn call after all of the addRow's are complete.

I worked up a quick live working example that is available at http://jsfiddle.net/UF8Ne/. See if you can follow that and understand where I am coming from.

Todd

Anthony R

  • Username: draftomatic
  • Joined: Fri Sep 17, 2010 2:01 pm
  • Posts: 15
  • Offline
  • Profile
Tags:

Re: Numbers being sorted like text

Post Posted: Thu Aug 18, 2011 1:51 pm
+0-
Ah!

It turned out to be an issue with the order I was doing things in. My code didn't guarantee that all of the addRow calls were completed when the sortColumn method was invoked.

And yes, I was initially confused about sortedBy but I had taken that out. dt.sortColumn applies the correct css as well. Unfortunately datasources aren't a great option for me, but good suggestion.

Thanks very much stlsmiths and others, you were a great help!
  [ 11 posts ] Go to page 1, 2 Next
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum