I am trying to sort a javascript array which my contains any type of data let say, numbers, strings, dates, times etc. My piece of logic is working well for all data types except numbers. It is considering number as string.
For example, I have a array like as follow:
array = ["1","2","12","22","33","3"]
I am expecting that sorted array would be 1,2,3,12,22,33…
but it is giving: 1,12,2,22,3,33…
Can anybody refine my code that must work for any type of data type.
Here is my piece of code for sorting:
function sortTable(a,b){
if(sortMode=="A"){
if(a[1]>b[1]) return 1;
if(a[1]<b[1]) return -1;
}
else if( sortMode=="D"){
if(a[1]>b[1]) return -1;
if(a[1]<b[1]) return 1;
}
return 0;
}
array.sort(sortTable);
Note: I cannot predict that which datatype of data will be in my array… so sorting function must be generic. Meaning, I cannot call different sort technique functions for different data types… Please make sure that sorting function should work for all data types.