2016-08-25 94 views
0

进出口使用剑道和打字稿我的应用程序和我有点IP地址行无法弄清楚如何排序与字符串表示的IPv4地址如何在剑道UI电网

排序必须看起来像这样

1.1.1.1 

10.1.1.1 

50.1.1.1 

100.1.1.1 

100.2.1.1 

255.255.255.255 

内gridColumns我已经试过这样的事情:

 { 
      field: "sourceIP", 
      title: this.$filter('translate')('tcMetadata.modules.widgets.tcConnectionsGrid.gridHeaders.sourceIP'), 
      type:"string", 
      sortable: { 
       compare: function(a,b){ 
        var arr = [that.ipaddressregex(a.sourceIP), that.ipaddressregex(b.sourceIP)]; 
        arr = arr.sort(); 
        console.log(arr, "ARRRRRRRRRRRRRRRRRR"); 
        return arr[0]; 
       } 
      } 
     } 

哪里that.ipaddressregex功能是:

public ipaddressregex(ip) { 
    ip = ip.split(".").map(function(i) { 
     return ("00"+i).slice(-3); 
    }).join("."); 
    ip = ip.replace(/\./g, ""); 
    return ip; 
} 

功能只是增加了零的空场,并移除点 12.10.1.1转化为012010001001

,然后javascript的sort()方法会照顾

但我认为我的问题是什么,我return在我的情况下return arr[0];

回答

1

您的比较函数应返回下面的伪代码中的值:

这是用来比较的值
function compare(a, b, descending) { 
     if (a is less than b by some ordering criterion) { 
     return -1; 
     } 

     if (a is greater than b by the ordering criterion) { 
     return 1; 
     } 

     // a must be equal to b 
     return 0; 
    } 

http://docs.telerik.com/kendo-ui/api/javascript/ui/grid#configuration-columns.sortable.compare

Javascript函数。它与Array.sort接受的比较函数具有相同的签名。

一个值得注意的例外是,我们还提供第三个参数,指示排序方向(真为降序)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

+0

Ÿ已经修好了,我意识到它必须返回1或0或-1,但谢谢 –