Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort array of objects by two properties

Tags:

sorting

swift

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.

like image 510
tobygriffin Avatar asked Apr 09 '15 05:04

tobygriffin


People also ask

How do you sort an array with two elements?

Step 1 : Here we can take two pointers type0 (for element 0) starting from beginning (index = 0) and type1 (for element 1) starting from end index. Step 2: We intend to put 1 to the right side of the array. Once we have done this then 0 will definitely towards left side of array to achieve this we do following.

How do you sort an array of objects?

To sort an array of objects, you use the sort() method and provide a comparison function that determines the order of objects.

How to sort an array of objects by multiple properties in JavaScript?

Sort an array of objects by multiple properties in JavaScript 1 Suppose, we have an array of objects like this − 2 We are required to sort the above array on the following criteria − 3 If dnf is true, 4 If issue is true, 5 Rest should be sorted by score, and if the scores are equal, by id

How does the sort() function work in Python?

So the sort () function will iteratively go through the array, comparing 2 elements at a time. In each iteration, a comparison is made between the city property of two objects. If the cities of two objects are the same, then their prices are also compared to decide the sort order.

How to order an array of objects by multiple values?

Here's how to order an array of objects by multiple property values. First the oldest. If they have the same age, first the tallest.

How to sort DNF-objects?

object goes to bottom; all dnf-objects should be sorted by score object goes to bottom, but above dnfs; all isCut-objects should be sorted by score Rest should be sorted by score, and if the scores are equal, by id


2 Answers

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  } 
like image 68
Dejan Skledar Avatar answered Sep 20 '22 15:09

Dejan Skledar


Here is a simple statement to do this sorting:

var sorted = array.sort { $0.isPriority == $1.isPriority ? $0.ordering < $1.ordering : $0.isPriority && !$1.isPriority } 
like image 20
John Avatar answered Sep 20 '22 15:09

John