Swift:按排序描述符排序数组

时间:2014-11-12 09:19:36

标签: ios arrays sorting core-data swift

我正在使用coredata所以我需要为我的实体排序描述符

例如,一个Coordinate-entity有这个类func:

class func sortDescriptors() -> Array<NSSortDescriptor>
{
    return [NSSortDescriptor(key: "sequence", ascending: true)]
}

我在对CoreData执行获取请求时使用此方法:

var request = NSFetchRequest(entityName: entityName)

request.sortDescriptors = T.sortDescriptors()

但是,当我将一个坐标数组作为另一个coredata对象的属性时,这是一个NSSet(即未排序的)

要解决这个问题,我将返回如下坐标:

return NSArray(array: coordinates!).sortedArrayUsingDescriptors(Coordinate.sortDescriptors()) as? Array<Coordinate>

使用NSArray获取sortedArrayUsingDescriptors方法感觉很难看。是否有类似的方法直接在Swift阵列上执行此操作,即。 Array<Coordinate>使用排序描述符?

谢谢!

3 个答案:

答案 0 :(得分:10)

没有内置方法,但您可以使用协议扩展名添加它们:

extension MutableCollectionType where Index : RandomAccessIndexType, Generator.Element : AnyObject {
    /// Sort `self` in-place using criteria stored in a NSSortDescriptors array
    public mutating func sortInPlace(sortDescriptors theSortDescs: [NSSortDescriptor]) {
        sortInPlace {
            for sortDesc in theSortDescs {
                switch sortDesc.compareObject($0, toObject: $1) {
                case .OrderedAscending: return true
                case .OrderedDescending: return false
                case .OrderedSame: continue
                }
            }
            return false
        }
    }
}

extension SequenceType where Generator.Element : AnyObject {
    /// Return an `Array` containing the sorted elements of `source`
    /// using criteria stored in a NSSortDescriptors array.
    @warn_unused_result
    public func sort(sortDescriptors theSortDescs: [NSSortDescriptor]) -> [Self.Generator.Element] {
        return sort {
            for sortDesc in theSortDescs {
                switch sortDesc.compareObject($0, toObject: $1) {
                case .OrderedAscending: return true
                case .OrderedDescending: return false
                case .OrderedSame: continue
                }
            }
            return false
        }
    }
}

但请注意,只有当数组元素是类而不是结构时才会起作用,因为NSSortDescriptor compareObject方法需要符合AnyObject的参数

答案 1 :(得分:10)

达米恩精彩扩展的另一种方法可能是多方投射。

myArray = (myArray as NSArray).sortedArrayUsingDescriptors(tableView.sortDescriptors) as! Array

原始来源:NSHipster

答案 2 :(得分:4)

@ Darniel的Swift 3版本的答案

<?php include('index2.php'); ?>
<?php include('db.php'); ?>
<?php
$mobile = $_SESSION['mobile'];
$date = date('M-d,Y H:i:s');
$date2 = date('M-d,Y');

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$amount = $_POST['amount'];
$balance = $_POST['balance'];

$balancenow = $balance - $amount; 

if ($amount > $balance) {
    $sql3 = "UPDATE users SET balance = '$balancenow'
    WHERE mobile = '$mobile'";        

    if ($conn->query($sql3) === TRUE) {
        echo  '<a href="index2.php"></a>';
    } else {
        echo "0";
    }
} else {
    echo '<script language="javascript">';
        echo 'alert("You don't have enough coupon balance")';
    echo '</script>';
    echo '<a href="index2.php"></a>';
} 
$conn->close();
?>