简体   繁体   English

使用Array.Sort而不在数组中同时使用两个值

[英]Using Array.Sort without using both values in array

I have an array like this: 我有一个像这样的数组:

var array = [{value: 1, text:"one"},{value: 2, text:"two"},{value: 3, text:"three"}]

and I want to sort it based on this variable: var order = '1,3,2'; 我想根据此变量对其进行排序: var order = '1,3,2';

Meaning, it will be in the order of 1,3,2 where its ordered by the value property of the object in the array. 意思是,它将按1,3,2的顺序排列,其中按数组中对象的value属性排序。

I am using it in an AngularJS ng-repeat array, but I cannot use the custom compare on the orderBy because I am using version 1.2.14 and it came out in 1.5.7 . 我在AngularJS ng-repeat数组中使用它,但是我无法在orderBy上使用自定义比较,因为我使用的是1.2.14版本,它在1.5.7中发布 I cannot change the version. 我无法更改版本。

I thought of extracting the logic out from AngularJS and just sorting the array in javascript , but am getting stuck because it sorts based on values in the array and not from another value. 我曾想过从AngularJS中提取逻辑,然后只在javascript中对数组进行排序,但是由于它是基于数组中的值而不是从另一个值中进行排序,因此陷入了困境。

For example, I have tried this with a custom sort but I only want to compare one index at a time. 例如,我已经尝试过使用自定义排序,但是我只想一次比较一个索引。 I don't want to loop through each pair of things to sort. 我不想遍历每对事物进行排序。 I only want to use a from the function and not both a and b . 我只想从功能中使用a ,而不要同时使用ab

 (function() { var app = angular.module('test', []); app.controller("test", function($scope) { $scope.selectedOption = {}; $scope.availableOptions = [{ value: 1, text: "one" }, { value: 2, text: "two" }, { value: 3, text: "three" }]; $scope.availableOptions.sort(sortOptions); }); var sortOptions = function(a, b) { var sortBy = '1,3,2'; var values = sortBy.split(','); for (val in values) { v = parseInt(val); if (v === a.value) { return 0; } else if (v < a.value) { return -1; } else if (v > a.value) { return 1; } } }; })(); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.14/angular.min.js"></script> <div ng-app="test" ng-controller="test"> <select ng-model="selectedOption.value"> <option ng-repeat="option in availableOptions" value="{{option.value}}">{{option.text}}</option> </select> </div> 

I would like the dropdown to have One, Three, Two in that order from top to bottom. 我想下拉有One, Three, Two的顺序从上到下。 Is there a way to only use .sort with only one value in the array at a time? 有没有一种方法只能一次在数组中只使用一个值的.sort And only compare it once? 只比较一次?

You can lookup the index of value in order . 您可以按order查找value索引。 It would be easier if order is an array , but if it must be a string, then you can still look up the index of in strings. 如果order是一个array ,会更容易,但是如果它必须是一个字符串,那么您仍然可以查找in的索引。

 let array = [{value: 1, text: "one"}, {value: 2, text: "two"}, {value: 3, text: "three"}]; let order = [1, 3, 2]; // '1,2,3' will also work let sorted = array.sort((a, b) => order.indexOf(a.value) - order.indexOf(b.value)); console.log(sorted); 

You could take a Map for the wanted sorting order, but you need both parameters for sorting, because you need to get the relative order. 您可以为所需的排序顺序选择一个Map ,但是由于需要获取相对顺序,因此需要两个参数进行排序。

 var array = [{ value: 1, text: "one" }, { value: 2, text: "two" }, { value: 3, text: "three" }], orderStr = '1,3,2', order = new Map(orderStr.split(',').map((k, v) => [+k, v])); array.sort((a, b) => order.get(a.value) - order.get(b.value)); console.log(array); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

为什么不添加诸如SortOrder之类的另一个属性,并按需要查看的顺序(从1,3,2到0,1,2)将其从0 ... N分配给数字,而不是将该字段用作orderBy字段没有任何特殊的自定义逻辑?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM