Not setting the attr or data of an element inside plugin

Not setting the attr or data of an element inside plugin

Guys,

I am writing a small plugin for myself and somehow inside the plugin , I am not able to set the value of the elements attribute inside the plugin method. I am trying to set the data-sortasc attribute of th element and although in the console log it shows it set it never does when I look at elements and inspect it. What could I be doing wrong. 

Here is code frag 

sort: function () {
            var $this = $(this), data = $this.data('tablerender');
            // If the plugin hasn't been initialized yet
            if (!data) {
                //Do more setup stuff here
                return this;
            }
            console.log(settings.dataField);
            console.log($(' th[data-realname=' + settings.dataField + ']', $this).attr('data-sortasc'));
            
            $(' th[data-realname=' + settings.dataField + ']', $this).attr('data-sortasc', "false");
            
            console.log($(' th[data-realname=' + settings.dataField + ']', $this).attr('data-sortasc'));
            
            data.tabledata.sort(customSort(data.sortDirection + data.sortField, data.sortFieldType));
            
            $this.data('tablerender', data);
            methods.render.apply(this);
}