views:

71

answers:

2

iam using following function in my javascript code

function AlphaSort(sort_type,cat_1,cat_2,cat_nm)
{
 var len = dataref.totalrow;
 var arr_name = new Array();

 for (var i =0; i<len; i++)
 { 
  var t = eval('dataref.mf_scheme['+i+'].'+sort_type);
  arr_name[i] = t;
 }

 arr_name.sort();
 if(sort_type!='scheme_name')
 {
  arr_name.reverse();
 }
 var j=0; 
 for (var i =0; i<arr_name.length; i++)
 {
  var sort_key = arr_name[i];

  for (j=i; j < arr_name.length; j++)
  {
   var next_sort_key = eval('dataref.mf_scheme['+j+'].'+sort_type);
   next_sort_key = next_sort_key;
   if (sort_key == next_sort_key)
   {
    break; 
   }
  }

  if (i != j)
  {
   var temp  = eval('dataref.mf_scheme['+i+']');
   eval('dataref.mf_scheme['+i+'] = dataref.mf_scheme['+j+']');
   eval('dataref.mf_scheme['+j+'] = temp' );
  }  
 }

 var s = '';

 for (var i =0; i<len; i++)
 {

  s += eval('dataref.mf_scheme['+i+'].'+sort_type)+',';
 }
 if((cat_1 != '') && (cat_2 != '') && (cat_nm != ''))
 {
  showUser(cat_1,cat_2,cat_nm);
 }
 else
 {
  showUser('1','1','all');
 }
}

where showUser is only printing the resulted array

function showUser(cats1,cats2,nam_cat)
{

 if((nam_cat=='all') && (cats1 !=1) && (cats2 !=1))
 {
  document.getElementById("all").style.display='';
  document.getElementById("eq").style.display='none';
 }
 else
 {
  var len = dataref.totalrow;
  var sc_nms = '';
  for (var i =0; i<len; i++)
  {
   var scm_id = eval('dataref.mf_scheme['+i+'].cat_id');
   var scm_mnths='';
   if((scm_id==cats1) || (scm_id==cats2) || (cats1==1) || (cats2==1))
   {
    var scm_name = eval('dataref.mf_scheme['+i+'].scheme_name');
    var scm_1mnth = eval('dataref.mf_scheme['+i+'].scheme_1_month');
    var arrs=new Array(scm_1mnth,scm_3mnth,scm_6mnth,scm_1yr,scm_3yr,scm_5yr,incept,navss);
    for (var j =0; j<arrs.length; j++)
    {
     var vals=arrs[j];
     scm_mnths +='<TD class="c3">'+vals+'</TD>';

    }
    sc_nms +='<TR class="ln hv"><TD class="l">'+scm_name+'</td>'+scm_mnths+'</tr>';
   }
  }
  document.getElementById("eq").style.display='';
  document.getElementById('eq').innerHTML = '<TABLE cellpadding="3" class="b bc r w4">'+sc_nms+'</Table>';
  document.getElementById("all").style.display='none';
 }
}

here arr_name.sort(); is sorting the function but my output is not correct

Output it is giving is like this :

90.45
9.90
81.89
49.67
43.59
202.99
18.10
165.46
111.17

which is wrong. It is sorting on the basis of first integer only not as a whole digit.

correct output would be :-

202.99
165.46
111.17
90.45
81.89
49.67
43.59
18.10
9.90

What function should i use and what is the problem with my existing code?????

EDIT :

all Schemes     1 mth  3 mth  6 mth  1 yr  3 yr  5 yr  
ICICI           -4.62   6.68 43.05 80.82 5.84 24.26 
Prudential       6.68 345.89 234.76 21.89 -10.23 9.90

This is the data that is coming in my array object.Now i want that if a user clicks 1 mnth then the whole data should be sorted 1 mnth wise and if a user clicks 3 year then whole data should get sorted according to that and so on. Now keeping this thing in mind where my code is going wrong?????

+1  A: 

Are those numbers, or strings? Convert them to numbers.

Knio
the behavior posted is clearly showing that the values are being treated as strings.
yoda
see iam pretty new to javascript so would you please specifically tell me where iam going wrong???my data can have anything alphabets in one array and then integers in the other.i have to do sorting of all these .How can i rectify my code????? plz help
developer
@developer - In your example, those objects are strings and are being sorted alphabetically, and the output is correct. If you want them sorted as numbers, they will have to be numbers in the array, so you will need to know that and convert them.
Knio
@knio : which object are you referring to?? if you are talking about my jason object then it is having data like thisall Schemes 1 mth 3 mth 6 mth 1 yr 3 yr 5 yr ICICI -4.62 6.68 43.05 80.82 5.84 24.26 Prudential 6.68 345.89 234.76 21.89 -10.23 9.90i cant make it integer but i want to sort it month wise that when i click 1 mnth then this whole data should be sorted 1 month data wise. This what i want and now im totally confuse :-(
developer
i also have edited my question
developer
+3  A: 

Actually your sort method is sorting the values lexically. use callbackFunction to sort it numerically.
Following code will sort your array as you are expecting.

arr_name.sort( callbackFunc );  // use callbackFunc wherever you are sorting 

function callbackFunc(a, b){
   return parseFloat(a) - parseFloat(b);
}
Rakesh Juyal
and somebody downvoted this as well :)
Rakesh Juyal