Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve unique data from table column and sorting array

There is one table , and I want the unique values of table column data, like table row may have 4, 2 , 2, 4,5 , 4. So it should return array like this data = [ 2 , 4 , 5 ]

I am able to achieve this using jquery as below link

http://jsbin.com/ojeroc/5/edit#source

But data should come in ascending order So I can use this array in another function.

$(document).ready(function(){
    console.log("document is ready");          
    var items=[], options=[];
    console.log(items);
    console.log(options);
    //Iterate all td's in second column
    $('#dataTable tr  td').each( function(){
    //console.log("Inside tr data");                                                                                                         
       //add item to array
       items.push( $(this).text() );       
      // console.log($(this).text());
    });
    //restrict array to unique items
    var items = $.unique( items );
    alert(items);
});
like image 737
Wazdesign Avatar asked Aug 07 '26 02:08

Wazdesign


1 Answers

function sortNumber(a,b)
{
      return a - b;
}

var n = ["10", "5", "40", "25", "100", "1"];
document.write(n.sort(sortNumber));

would create this output:

1,5,10,25,40,100

You could also wrap it in an anonymous function

n.sort(function(a,b){return a-b;})

Which would be faster, but if you need to use it over and over again, I would stick with the function.

like image 145
mas-designs Avatar answered Aug 09 '26 16:08

mas-designs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!