开发者

array.sort() is not giving the desired result

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_6mnt开发者_开发知识库h,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?????


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);
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜