2017-10-27 8 views
0

私のHTMLファイルanglejsを使ってこのデータをカスタムソートする方法は?

<div ng-controller="MyCtrl">  
<input type="text" ng-model="searchText" /> 
<ul ng-repeat="strVal in arrVal|filter:searchText|mySort" > 
<li>{{strVal}}</li> 
    </ul></div> 

私のjsファイル

var app=angular.module('myApp', []); 
app.controller('MyCtrl', function ($scope,$filter) { 
    $scope.arrVal = ['six','one','two','five','three','four']; 
}); 

app.filter('mySort', function() { 
return function(input) { 
    return input.sort(); 
} 
    }); 

が、結果は、私はそれが私の最終的な出力であると思います。この結果

five//but it is order by A-Z, I am very unhappy 
four 
one 
six 
three 
two 

出力したくない、ということです結果、任意のアイデア

one 
two 
three 
four 
five 
six 

そんなに

Fiddle

答えて

1

THXは、私はこのSO Postを見つけて、

app.filter('mySort', function() { 

var Small = { 
    'zero': 0, 
    'one': 1, 
    'two': 2, 
    'three': 3, 
    'four': 4, 
    'five': 5, 
    'six': 6, 
    'seven': 7, 
    'eight': 8, 
    'nine': 9, 
    'ten': 10, 
    'eleven': 11, 
    'twelve': 12, 
    'thirteen': 13, 
    'fourteen': 14, 
    'fifteen': 15, 
    'sixteen': 16, 
    'seventeen': 17, 
    'eighteen': 18, 
    'nineteen': 19, 
    'twenty': 20, 
    'thirty': 30, 
    'forty': 40, 
    'fifty': 50, 
    'sixty': 60, 
    'seventy': 70, 
    'eighty': 80, 
    'ninety': 90 
}; 

var Magnitude = { 
    'thousand':  1000, 
    'million':  1000000, 
    'billion':  1000000000, 
    'trillion':  1000000000000, 
    'quadrillion': 1000000000000000, 
    'quintillion': 1000000000000000000, 
    'sextillion': 1000000000000000000000, 
    'septillion': 1000000000000000000000000, 
    'octillion': 1000000000000000000000000000, 
    'nonillion': 1000000000000000000000000000000, 
    'decillion': 1000000000000000000000000000000000, 
}; 

var a, n, g; 

function text2num(s) { 
    a = s.toString().split(/[\s-]+/); 
    n = 0; 
    g = 0; 
    a.forEach(feach); 
    return n + g; 
} 

function feach(w) { 
    var x = Small[w]; 
    if (x != null) { 
     g = g + x; 
    } 
    else if (w == "hundred") { 
     g = g * 100; 
    } 
    else { 
     x = Magnitude[w]; 
     if (x != null) { 
      n = n + g * x 
      g = 0; 
     } 
     else { 
      alert("Unknown number: "+w); 
     } 
    } 
} 

    return function(input) { 
    console.log(input); 
     return input.sort(function(a, b) { 
    return text2num(a) - text2num(b); 
}); 
    } 
    }); 

Working fiddle

のように、この作業何かを得るしている番号にあなたの言葉を変換する必要があり、それを行うためにどのように任意のアイデアに
0

このようにしてください。

var app = angular.module('myApp', []); 
 

 
app.controller('myController', function($scope, $filter) { 
 
    $scope.arrVal = ['six','y','one','x','two','five','three','four','z']; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular.min.js"></script> 
 
<div ng-app="myApp"> 
 
    <div ng-controller='myController'> 
 
     <input type="text" ng-model="searchText" /> 
 
    <ul ng-repeat="strVal in arrVal|filter:searchText | orderBy:'':'true'" > 
 
     <li>{{strVal}}</li> 
 
    </ul> 
 
    </div> 
 
</div>

関連する問題