如何有效地按相反顺序对对象数组进行排序?

时间:2022-01-21 16:44:52

Suppose I have an array of dictionaries:

假设我有一系列字典:

[ { "id": 2 }, { "id": 59 }, { "id": 31 } ... ]

How can I sort this so that it's in descending order, sorted by "id"?

我怎样才能对它进行排序,使其按降序排列,按“id”排序?

My initial approach is something like:

我最初的方法是这样的:

Loop through each element, find the biggest one, and put it into a new array. Then, remove that from the element. Repeat.

But I know that's wrong and not efficient.

但我知道这是错误的,效率不高。

4 个解决方案

#1


5  

You can use the sort function in Swift. Something like this:

您可以在Swift中使用sort函数。像这样的东西:

let arr = [["id": 2], ["id": 59], ["id": 31]]
let sortedArr = arr.sort { Int($0["id"]!) > Int($1["id"]!) }

#2


1  

You have only need to use below code that will give you the sorted array using the sort because dictionary is also a collection class.

您只需要使用下面的代码,它将使用排序为您提供排序数组,因为字典也是一个集合类。

let sortedDict = wordDict.sort { $0.0 < $1.0 }
print("\(sortedDict)") // 

In above code you found the sorted Dictionary. For sorting Array you need to add below code.

在上面的代码中,您找到了排序的Dictionary。对于排序数组,您需要添加以下代码。

let sortedArr = arr.sort { Int($0["id"]!) > Int($1["id"]!) }

#3


0  

I don't know if it is the fastest or the better,but swift provides the sort method that takes a clojure as an argument. You can write something like that:

我不知道它是最快还是更好,但是swift提供了将clojure作为参数的sort方法。你可以这样写:

var ordered = arrdict.sort { (dic1, dic2) -> Bool in
    dic1["id"] > dic2["id"]
}

Or compact:

var ordered = arrdict.sort { $0["id"] > $1["id"] }

The $0..x makes possible to access clojure arguments.
About sort algorithm Apple write:

$ 0..x可以访问clojure参数。关于排序算法Apple写道:

Discussion The sorting algorithm is not stable (can change the relative order of elements that compare equal).

讨论排序算法不稳定(可以改变比较相等的元素的相对顺序)。

Requires: The less-than operator (func <) defined in the Comparable conformance is a strict weak ordering over the elements in self.

要求:Comparable一致性中定义的less-than运算符(func <)是对self中元素的严格弱排序。

#4


0  

    var val = [ { "id": 2 }, { "id": 59 }, { "id": 31 }];
var a = new Array();


for (i = 0; i < val.length; i++) { 
    document.write(Object.keys(val[i]).map(function (key) {return val[i][key]})+"<br>");
    a.push(Object.keys(val[i]).map(function (key) {return val[i][key]}));
}
a.sort();

var result = new Array();
for (i = 0; i < val.length; i++) { 
result.push({"id": a[i]});
}

#1


5  

You can use the sort function in Swift. Something like this:

您可以在Swift中使用sort函数。像这样的东西:

let arr = [["id": 2], ["id": 59], ["id": 31]]
let sortedArr = arr.sort { Int($0["id"]!) > Int($1["id"]!) }

#2


1  

You have only need to use below code that will give you the sorted array using the sort because dictionary is also a collection class.

您只需要使用下面的代码,它将使用排序为您提供排序数组,因为字典也是一个集合类。

let sortedDict = wordDict.sort { $0.0 < $1.0 }
print("\(sortedDict)") // 

In above code you found the sorted Dictionary. For sorting Array you need to add below code.

在上面的代码中,您找到了排序的Dictionary。对于排序数组,您需要添加以下代码。

let sortedArr = arr.sort { Int($0["id"]!) > Int($1["id"]!) }

#3


0  

I don't know if it is the fastest or the better,but swift provides the sort method that takes a clojure as an argument. You can write something like that:

我不知道它是最快还是更好,但是swift提供了将clojure作为参数的sort方法。你可以这样写:

var ordered = arrdict.sort { (dic1, dic2) -> Bool in
    dic1["id"] > dic2["id"]
}

Or compact:

var ordered = arrdict.sort { $0["id"] > $1["id"] }

The $0..x makes possible to access clojure arguments.
About sort algorithm Apple write:

$ 0..x可以访问clojure参数。关于排序算法Apple写道:

Discussion The sorting algorithm is not stable (can change the relative order of elements that compare equal).

讨论排序算法不稳定(可以改变比较相等的元素的相对顺序)。

Requires: The less-than operator (func <) defined in the Comparable conformance is a strict weak ordering over the elements in self.

要求:Comparable一致性中定义的less-than运算符(func <)是对self中元素的严格弱排序。

#4


0  

    var val = [ { "id": 2 }, { "id": 59 }, { "id": 31 }];
var a = new Array();


for (i = 0; i < val.length; i++) { 
    document.write(Object.keys(val[i]).map(function (key) {return val[i][key]})+"<br>");
    a.push(Object.keys(val[i]).map(function (key) {return val[i][key]}));
}
a.sort();

var result = new Array();
for (i = 0; i < val.length; i++) { 
result.push({"id": a[i]});
}