Javascript 如何按数字和字符排序

Javascript 如何按数字和字符排序,javascript,html,angularjs,angularjs-orderby,Javascript,Html,Angularjs,Angularjs Orderby,我有一个ng repeat,下拉列表中有以下值 Up to 6 months From 13 to 24 months Higher than 24 months From 6 to 12 months 我需要对这些值进行如下排序 //following is the expected output // Up to 6 months //From 6 to 12 months //From 13 to 24 months //Higher than 24 months $scope.

我有一个
ng repeat
,下拉列表中有以下值

Up to 6 months
From 13 to 24 months
Higher than 24 months
From 6 to 12 months
我需要对这些值进行如下排序

//following is the expected output
// Up to 6 months
//From 6 to 12 months
//From 13 to 24 months
//Higher than 24 months




$scope.items = [{name: 'Up to 6 months', id: 30 },{ name: 'From 13 to 24 months', id: 27 },{ name: 'Higher than 24 months', id: 50 },{ name: 'From 6 to 12 months', id: 50 }];

您的名称字段不是格式化文本,您无法按名称排序,请添加一个如下字段,该字段将使用排序条件填充字段值。此处给出了一个示例排序函数:

$scope.items.sort(
function(a,b) {
// Change the logic based on your criteria
return (/*Your criteria*/) ? 1 : 0);
} 
);
您的数据如下所示:

$scope.items = [{name: 'Up to 6 months',sort:0, id: 30 },{ name: 'From 13 to 24 months',sort:1, id: 27 },{ name: 'Higher than 24 months',sort:3, id: 50 },{ name: 'From 6 to 12 months', sort:4, id: 50 }];
使用以下命令:

<div ng-repeat="item in items | orderBy:['sort','id']">{{item.name}}-{{item.id}}</div>
{{item.name}-{{item.id}

您的物品将像这样装载

 $scope.selectedItem = { name: 'two', id: 27 };
 $scope.items = [{name: 'Up to 6 months',sort:1, id: 30 },{ name: 'From 13 to 24 months',sort:3, id: 27 },{ name: 'Higher than 24 months',sort:4, id: 50 },{ name: 'From 6 to 12 months', sort:2, id: 50 }];
ng重复

<select ng-model="selectedItem">
   <option ng-repeat="item in items  | orderBy:['sort']" value={{item.id}}>
    {{item.name}}
   </option>
</select>

{{item.name}

您不能在每个项目中设置排序顺序,然后按该排序顺序进行排序吗?我需要根据名称值在逗号分隔字符串“最多6个月,从6个月到12个月,从13个月到24个月,高于24个月”中的位置对每个项目进行排序。我希望这不是它在数据库中的存储方式-您如何处理将“超过24个月”更改为“超过两年”的请求?似乎更好,但顺序有点变化//从13个月更改为24个月/最多更改为6个月//从6个月更改为12个月//高于24个月检查更新的答案您维护了另一个排序id。。。,这些值来自db。。。所以我需要维护另一个专栏。。。。这是困难的。必须根据名称字段创建密钥。由于名称不是按字母顺序排列的,因此无法在该字段上按顺序排列,因此必须创建一个键。