如何在angularJs中进行自然排序? [英] How to do Natural sorting in angularJs?
问题描述
我下面有一个这样的对象:
I have an object like this below:
scope.obj=[{option :"option 1"
},{option :"option 2"
},{option :"option 3"
},{option :"option k"
}]
和我的html
<a ng-repeat="item in obj | orderBy :'option':false">{{item.option}}</a>
,并希望以升序显示对象列表,如下所示:
and expecting to display the object list in ascending order like below:
option k
option 1
option 2
option 3
但输出为
option 1
option 2
option 3
option k
我尝试了在stackoverflow和其他站点中提供的许多解决方案,但是没有任何效果.请帮我解决这个问题
I have tried with many solution given in stackoverflow and other sites but nothing is working. Pls help me to solve this issue
我的期望是,如果在选项"字符串(例如选项k,选项L等)之后有任何字符应按升序排在首位,然后数字应排在首位(选项k,选项L,选项1,选项2等)./p>
My expectation is if any character after "option" string (like option k,option L etc) there should come first in ascending order then number should come(option k,option L,option 1,option 2,etc)
推荐答案
我通过用空格将字符串分成2部分,将第二部分转换为整数(如果它是"k",给出-1作为值),然后解决了这个问题转换后的字符串的第二部分对数组进行排序.代码如下所示
I solved this by spliting the string into 2 parts by Space,converting the 2nd part to integer ,if it is "k" giving -1 as value and then sorting array by converted 2nd part of the string.code is given below
for ( var i = 0; i < scope.obj.length; i++) {
var option= scope.obj[i].option.split(" ");
scope.obj[i].optionValue = parseInt(optionValueSet(option[1]));
}
function optionValueSet(value) {
if (value=== "K"){
return -1;
}
return value;
}
和我的html
<a ng-repeat="item in obj | orderBy :'optionValue':false">{{item.option}}</a>
这篇关于如何在angularJs中进行自然排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!