Select column and sort when click header's dropdown in a table

Now I am trying to sort for each column in a table and I put it dropdown menu beside each header to sort for it but my column header are dynamically produced so I cant select and sort for each column.
My codes are as below

var rows = $(’#tblDataListGrid tbody tr’).get();
var col = 1;
rows.sort(function (a, b) {

                var A = $(a).children('td').eq(col).text().toUpperCase();
                var B = $(b).children('td').eq(col).text().toUpperCase();

                if (A < B) {
                    return -1;

                if (A > B) {
                    return 1;

                return 0;


            $.each(rows, function (index, row) {

My question is how can I select column dynamically and I think I must replace “eq(col)” to be dynamic .How about your suggestions.

Thank you