本文介绍了如何对元组数组进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何实现(或创建)元组列表的数组排序?
从我的代码中收集了以下内容.

基本上,我创建了一个元组数组并通过for循环填充它;之后我尝试对其进行排序.

How do you implement (or create) an array sort of a list of tuples?
The following was gleaned from my code.

Essentially I created an array of tuplesand populated it via for loop; after which I tried to sort it.

var myStringArray: (String,Int)[]? = nil
...

myStringArray += (kind,number)
...

myStringArray.sort{$0 > $1}

这是Xcode在我可以构建之前给我的:

This is what Xcode gave me before I could build:

推荐答案

您有两个问题.首先,myStringArrayOptional,必须先对其进行包装",然后才能在其上调用方法.其次,元组没有>运算符,您必须自己进行比较

You have two problems. First, myStringArray is an Optional, you must "unwrap" it before you can call methods on it. Second, there is no > operator for tuples, you must do the comparison yourself

if let myStringArray = myStringArray {
    myStringArray.sort { $0.0 == $1.0 ? $0.1 > $1.1 : $0.0 > $1.0 }
}

这篇关于如何对元组数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 16:35