Sort array of objects by two properties

tobygriffin

This has been asked and answered before using NSSortDescriptor where it is quite easy. But is there a Swift-standard way using Array.sort()?

struct Sortable {
    let isPriority: Bool
    let ordering: Int
}

Sorting an array of Sortables by one property is simple:

sort { $0.ordering < $1.ordering }

But I want to sort by isPriority then by ordering - and I can't get my head around a simple statement to make that happen.

Dejan Skledar

Yes there is a very simple way using the Array.sort()

Code:

var sorted = array.sorted({ (s1, s2) -> Bool in
    if s1.isPriority && !s2.isPriority {
        return true //this will return true: s1 is priority, s2 is not
    }
    if !s1.isPriority && s2.isPriority {
        return false //this will return false: s2 is priority, s1 is not
    }
    if s1.isPriority == s2.isPriority {
        return s1.ordering < s2.ordering //if both save the same priority, then return depending on the ordering value
    }
    return false
})

The sorted array:

true - 10
true - 10
true - 12
true - 12
true - 19
true - 29
false - 16
false - 17
false - 17
false - 17
false - 18

Another a bit shorter solution:

let sorted = array.sorted { t1, t2 in 
   if t1.isPriority == t2.isPriority {
      return t1.ordering < t2.ordering 
   }
   return t1.isPriority && !t2.isPriority 
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

PHP sort array of objects by two properties

Sort an array of objects by two properties, but force one to go first

Sort an array of objects by multiple properties

Sort array by two properties in angular

How to sort an array of objects using two properties but conditionally group in the sort order depending on a condition?

JavaScript sort an array of objects based array of properties

How to sort list of objects by two properties and collator

Sort array of objects in javascript with two sort conditions

How to sort an array of objects without sequentially repeating two specific properties in Javascript?

Sort array of objects using multiple date properties

How to sort array of objects by its boolean properties

Javascript - Sort Array of objects by 2 Properties

How to sort array of objects have date properties?

JavaScript - Sort array by two integer properties

How to sort an array using Array.prototype.sort() by two properties?

Removing objects from array based on two properties

Merge two array of objects based on matching properties

Search array of objects (two properties) for multiple restrains

how to group an array of objects based on two properties

Sort objects in an array by two different criteria

Ruby: Sort array of objects after an array of name properties

Apply array.sort() default algorithm to string properties of objects in an array

Combining different properties of two different arrays of objects in one array of objects

how to sort an array of objects using the value of the objects two times consecutively

sort two dimensional array of objects based on name or sort array of array of objects based on name

How to sort array of objects by properties with only one function?

JavaScript sort array of objects on multiple properties by specific constant values

How to sort by increasing order an array of objects based on its properties - Swift

Javascript-- sort an array of objects with a specified order of object properties