Sorting numbers

Sorting numbers

Philip HarveyPhilip Harvey Posts: 23Questions: 0Answers: 0
edited April 2009 in General
Hi there, my apologies if this has been discussed but just starting with this great program. I need to sort a column that contains a set of numbers such as 1,5,9,10. But when sorted it comes out 1,10,5,9. It looks as if it only looks at the first digit, have I missed something obvious ?

Many thanks in advance
Phil

Replies

  • allanallan Posts: 63,578Questions: 1Answers: 10,483 Site admin
    edited April 2009
    Hi Phil,

    It sounds like the sort is being done alphabetically, rather than by number. DataTables should automatically detect numbers (as seen in column four of the example at http://www.datatables.net ), so it sounds like you probably have a non-numeric character in that column. It may be white space, or perhaps a printable non-numeric characters.

    Hope this helps.
    Allan
  • Philip HarveyPhilip Harvey Posts: 23Questions: 0Answers: 0
    Thanks Allan, I'll have a look at that
  • Philip HarveyPhilip Harvey Posts: 23Questions: 0Answers: 0
    Hi Allan,

    I have had a look at the numbers being passed to the column and added a php trim() statement
    around the variable to strip off any white space but still cant get it to sort numerically
    I was looking at some of the sort API stuff (not that I really understand it) should I define this column
    as numeric, would that help ?

    The only other thing I need to fix is the colours for the row striping, I apologise for my lack of CSS knowledge
    but could you send me the CSS line numbers that set the colours for thr odd / even rows and the table headers.
    I assume that the colours used for highlighting colums & rows are derived from these rows.

    Many thanks

    Phil
  • allanallan Posts: 63,578Questions: 1Answers: 10,483 Site admin
    Hi Phil,

    1. The column should automatically be detected as numeric if it only has the characters "0123456789." in it (and the . only once). Otherwise it will be sorted as a string as you have found. Can you provide a link to a sample page where the sorting doesn't auto-detected for you?

    2. The CSS for the rows is nice and simple:

    [code]
    .odd {
    background-color: red;
    }

    .even {
    background-color: blue;
    }
    [/code]

    Likewise for the header elements, if you look for 'th' in the demo styles file you will see where those classes are defined.

    Hope this helps,
    Allan
  • Philip HarveyPhilip Harvey Posts: 23Questions: 0Answers: 0
    Hi Allan

    Here is the link http://www.test.horizondevelopers.co.uk/H2030T/H2030_Plan.php

    It is in the column named miles

    Many thanks for your help with this
    Phil
  • allanallan Posts: 63,578Questions: 1Answers: 10,483 Site admin
    Hi Phil,

    Thanks for the link - it does look like the white space in your table is causing the problem here:

    [code]

    7.5
    [/code]

    The easiest thing to do to fix this is to remove this white space from the output. Alternatively you might try using fnRender() to remove the white space using trim(), or finally you can modify the numeric sort (and type detection) to consider white space (it doesn't do this by default because it is inefficient doing it this way).

    Allan
  • Philip HarveyPhilip Harvey Posts: 23Questions: 0Answers: 0
    Hi Allan, yes, I can see the white space if you do view source.. but I have a trim() statement around the field and have tried doing ">" . $var . "<" and that shows >7.5< so not quite sure where the problem is, any ideas ?. I will have to look at fnRender, I have not come across that yet

    Phil
  • Philip HarveyPhilip Harvey Posts: 23Questions: 0Answers: 0
    edited April 2009
    Hi Allan, you were right

    <?php echo "" . trim($row['elen']) ."" ; ?> ---- this works
    <?php echo trim($row['elen']); -- this does not, I'm not sure why

    Anyway, thanks Allan, I just need to get to grips with the CSS and I'm done :-)

    Just a thought, is there an option to expand / contract the entry when the text for a field is longer than the field length ?. At the moment it wraps which is cool but would be nice to see an option to expand / contract long entries

    Many thanks
    Phil
  • allanallan Posts: 63,578Questions: 1Answers: 10,483 Site admin
    Hi Phil,

    Good stuff - glad to hear it works. This should also do the trick (assuming the trim is unnecessary):

    [code]
    <?=$row['elen'];?>
    [/code]

    Regarding your second point - You might be able to do something clever with Javascript do do that. Another option is using details columns: http://datatables.net/1.5-beta/examples/api/row_details.html - or altering the width of columns (although DataTables should automatically pick the optimal width), or finally using ellipsis (support for which is very poor in many browsers...).

    Many options to choose from :-)

    Allan
This discussion has been closed.