Swift 2.0 Sorting Array of Objects by Property

Swift 2.0 Sorting Array of Objects by Property

In Swift 2:

  • You can use sort method, using compare to compare the two dates:

    let sortedArray = myArray.sort { $0.myDate.compare($1.myDate) == .OrderedAscending }  // use `sorted` in Swift 1.2
  • Or, if you want to sort the original array, you can sortInPlace:

    myArray.sortInPlace { $0.myDate.compare($1.myDate) == .OrderedAscending }  // use `sort` in Swift 1.2

In Swift 3:

  • to return a sorted rendition of the array, use sorted, not sort

    let sortedArray = myArray.sorted { $0.myDate < $1.myDate }
  • to sort in place, it's now just sort:

    myArray.sort { $0.myDate < $1.myDate }

And with Swift 3's Date type, you can use the < operator.

How to sort multiple class objects with date property in single array with swift?

Add the date property to your protocol:

protocol MyType {
var date: String? { get }
}

Then you can sort the array with something similar to:

array.sort { $0.date < $1.date }

or sort it in place with

array.sortInPlace { $0.date < $1.date }

Note that the string format your using allows you to use lexicographic sorts to achieve date sorted order, otherwise you might want to store the dates as NSDates.

.sort not working in Swift 2.0

In Swift 2, what was sort is now sortInPlace (and what was sorted is now sort), and both methods are to be called on the array itself (they were previously global functions).

When you call combination.sort({$0 < $1}) you actually return a sorted array, you're not sorting the source array in place.

And in your example the result of combination.sort({$0 < $1}) is not assigned to any variable, that's what the compiler is telling you with this error message.

Assign the result of sort:

let sortedArray = combination.sort({$0 < $1})
print(sortedArray)

If you want to get an array of sorted arrays, you can use map instead of a loop:

let myCombinations = [["lys", "dyt", "lrt"], ["lys", "dyt", "gbc"], ["lys", "dyt", "lbc"]]

let sortedCombinations = myCombinations.map { $0.sort(<) }

print(sortedCombinations) // [["dyt", "lrt", "lys"], ["dyt", "gbc", "lys"], ["dyt", "lbc", "lys"]]

Swift. Sort array of struct

Your code works fine when you tested in a Playground in the following way:

struct SoundTrack {
let sID : Int
let st : String
}

var aSoundTracks_Filtered = [SoundTrack]()

aSoundTracks_Filtered.append(SoundTrack(sID: 1, st: "a"))
aSoundTracks_Filtered.append(SoundTrack(sID: 2, st: "b"))

aSoundTracks_Filtered.sort{ $0.st > $1.st } // [{sID 2, st "b"}, {sID 1, st "a"}]

But sort() sorts an array in-place. What you probably want to use is sorted(), which does not modify the original array and returns a new sorted array:

let aRes = aSoundTracks_Filtered.sorted{ $0.st > $1.st }

The above code is for Swift 1.2, for Swift 2.0 returning a sorted array is called "sort" again, but it is a (protocol extension) method now instead of a global function. I hope this help you.

Swift - Get Object in Array By Property

So Swift provides your a way to filter a list of object based on the condition you want.

In this case, you will need to use filter function:

class func GetPosesById(Id: Int) -> YogaPose?{
return listPoses.filter({ $0.id == Id }).first
}

Basically, the filter function will loop thru the entire listPoses and returns you a [YogaPose]. The code ({$0.id == Id}) is your condition and $0 means the current object in the loop.

I also change your function signature a bit

class func GetPosesById(Id: Int) -> YogaPose

To

class func GetPosesById(Id: Int) -> YogaPose?

because the first property is an optional object which you will need to unwrap later



Related Topics



Leave a reply



Submit