How to find the max value in a column

How to find the max value in a column

FuzzyBunnyFeetFuzzyBunnyFeet Posts: 10Questions: 1Answers: 0

I'm trying to write a function to return the max value in a table column which is identified by a selector that is unique to the table but may not be unique on the page. Here is what I have which isn't working (I've commented alternatives that I have tried):

function maxIntValue(table, colSelector) {
    var currentValue = 0;
    var highValue = 0;
//    table.column("th " + colSelector).each(function (columnIndex) {
    table.column("th " + colSelector).cell().each(function (columnIndex) {
        $(this).find("td:eq(" + columnIndex + ")").each(function () {
        //    currentValue = parseInt($(this).html());
            currentValue = parseInt($(this).text());
            if (currentValue != "NaN") {
                highValue = Math.max(currentValue, highValue);
            }
        });
    });
    return highValue;
}

I have a jsFiddle at http://jsfiddle.net/FuzzyBunnyFeet/WTXM7/42/

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    This is the simplest way I can think of doing it:

        var nextSeqNum = exampleTable
            .column('.seqNum')
            .data()
            .sort()
            .reverse()[0];
    

    i.e. get the data for your column, sort it and reverse so the largest is at the start and take that.

    I've got the chain on separate lines to make it obvious what is being called, but it could be a one liner if you wanted.

    http://jsfiddle.net/WTXM7/44/

    Allan

  • FuzzyBunnyFeetFuzzyBunnyFeet Posts: 10Questions: 1Answers: 0
    edited May 2014

    I updated my jsFiddle to continue using a more general function which returns a number instead of a string.

    http://jsfiddle.net/FuzzyBunnyFeet/WTXM7/53/

This discussion has been closed.