Table is displaying in reverse alphabetical order. How do I get it in a-z order?

Table is displaying in reverse alphabetical order. How do I get it in a-z order?

mac173173mac173173 Posts: 16Questions: 3Answers: 0

The JSON table is not in order, but is displayed in reverse. The table originally had a first column showing 'Yes' or 'No' and was displaying in that order. I have removed that column. The page is not yet live, so I cannot link it here. Below is the script that drives the table.

<script>
        $(document).ready(function() {

            jQuery.extend( jQuery.fn.dataTableExt.oSort, {
                "my-currency-pre": function(a) {
                    return parseFloat(a.replace(/[ $,]/gi, ''));
                },
                "my-currency-asc": function(a,b) {
                    return ((a < b) ? -1 : ((a > b) ? 1 : 0));
                },
                "my-currency-desc": function(a,b) {
                    return ((a < b) ? 1 : ((a > b) ? -1 : 0));
                }
            });


                $('#example').DataTable( {
                    "ajax": '/careerconnections/data/DataTables/demand_occupations_list_2020.txt',
                    "order": [0],
                    "lengthMenu": [ 10, 25, 50, 75, 100 ],
                    "aoColumnDefs": [
                      {"sType": "my-currency", "aTargets": [3,4,5]}
                    ]
                } );
        } );
    </script>

First few lines of the JSON

{
    "data":[
  ["Compliance Officers, Except Agriculture, Construction, Health and Safety, and Transportation","12,340","$83,205","600","970","Bachelor's degree"  ],
  ["Cost Estimators","5,270","$75,339","650","610","Bachelor's degree"  ],
  ["Human Resources Specialists","17,510","$75,357","1,250","1,800","Bachelor's degree"  ],
  ["Logisticians","7,530","$84,241","750","790","Bachelor's degree"  ],
  ["Management Analysts","14,340","$105,676","1,950","1,770","Bachelor's degree"  ],
  ["Meeting, Convention, and Event Planners","3,060","$65,953","550","490","Bachelor's degree"  ],
  ["Market Research Analysts and Marketing Specialists","19,330","$87,212","4,050","2,470","Bachelor's degree"  ],

On the page, the first item showing in the table is Welders, Cutters, Solderers, and Brazers.

This question has accepted answers - jump to:

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    I don't really understand the question; but why don't you just apply the initial order of your choice as explained in the docs?
    https://datatables.net/reference/option/order

  • mac173173mac173173 Posts: 16Questions: 3Answers: 0
    edited July 2020

    I should have mentioned, I am not a coder. I maintain and update this website, but the coder was hired, and I cannot pay for edits.
    So, if I edited the order line to

    "order": [0, asc],
    

    should that fix it?

    Also, page is now live.
    https://careerconnections.nj.gov/careerconnections/prepare/skills/demand/demand_occupations_list.shtml

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    Answer ✓

    Yes - if you want default sorting to be by your first column, A-Z.
    Column numbering begins with 0, left to right.

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923
    edited July 2020 Answer ✓

    "order": [0, asc],

    The order docs state this:

    This 2D array structure allows a multi-column order to be defined as the initial state should it be required.

    You should have "order": [[0, 'asc']], instead. Also the docs state the default value is [[0, 'asc']] so the first column should be sorted ascending by default without the need of "order": [[0, 'asc']],.

    Kevin

  • mac173173mac173173 Posts: 16Questions: 3Answers: 0

    kthorngren, the original code was

    "order": [0]
    

    and this produced the reverse order. I changed it to

    "order": [0, 'asc']
    

    as tangerine suggested and it is now displaying correctly. Thank you for your answer, I will change it as you said and see if it still works. Although I am not a coder, I do try to learn.

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    You should have "order": [[0, 'asc']], instead.

    Kevin is correct, my mistake.

  • mac173173mac173173 Posts: 16Questions: 3Answers: 0

    I changed it and it is working.

    Thank you both very much.

This discussion has been closed.