1

i have a problem on javascript reverse method when all the data to be sorted are equal.

here is my code:

var ascAgt = false;
        function sortTableAgt() {
            var tbl = document.getElementById("mytable").tBodies[0];
            var store = [];
            for (var i = 0, len = tbl.rows.length; i < len; i++) {
                var row = tbl.rows[i];
                var rowdatedata = row.cells[1].innerHTML;
                store.push([rowdatedata, row]);
            }
            if (ascAgt) {
                store.sort();
                ascAgt = false;
            } else {
                store.sort();
                store.reverse();
                ascAgt = true;
            }
            for (var i = 0, len = store.length; i < len; i++) {
                var idno = i + 1;
                store[i][1].cells[0].innerHTML = idno.toString();
                tbl.appendChild(store[i][1]);
            } store = null;
        }

sample data:

ID  Name    Date
1   XXXXX   2012/10/05
2   XXXXX   2012/03/16
3   XXXXX   2012/05/18
4   XXXXX   2012/05/18

as i sort the name part,what i want is that nothing happens because names are equal but when it comes to the reverse method it will just reversed the given data. am i missing something here? thank you in advance....

1
  • How do you expect sort to work on an array containing arrays? Same with reverse. Commented Oct 25, 2012 at 12:49

1 Answer 1

2

So, what you want is to sort by 2nd column and, if the values are equal, sort also by 3rd column. Right?

The sort() function takes also a callback. You have to use that.

var ascAgt = false;

function sortTableAgt() {
  var table = document.getElementById("mytable").tBodies[0],
      rows = table.rows,
      len = rows.length,
      i = 0,
      store = [],
      order = ascAgt ? 1 : -1;

  for (i = 0; i < len; i++) {
    store.push(rows[i]);
  }

  store.sort(function(a, b) {
    if (a.cells[1].innerHTML == b.cells[1].innerHTML) {

      // If 2nd cell are equal, sort by 3rd cell
      // Compare them as date (timestamp)
      var dateA = new Date(a.cells[2].innerHTML).getTime(),
          dateB = new Date(b.cells[2].innerHTML).getTime();

      if (dateA == dateB) {
        return 0;
      } else if (dateA < dateB) {
        return -1 * order;
      } else {
        return 1 * order;
      }

    } else if (a.cells[1].innerHTML < b.cells[1].innerHTML) {
      return -1 * order;
    } else {
      return 1 * order;
    }
  });

  for (i = 0; i < len; i++) {
    store[i].cells[0].innerHTML = (i+1).toString();
    table.appendChild(store[i]);
  }
}

You can also check tis demo in JSBin: http://jsbin.com/welcome/39336/edit

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.