Specify multiple sort conditions in Swift

When I wanted to specify two sorting conditions, I was wondering how to write it for about an hour. There is no similar article on Qiita, and above all, I was impressed with "I should write like this !!", so please share it.

import Foundation

// Structure that holds weight and height
struct Person {
   let seq: Int
   let weight: Int
   let height: Int
   init(seq: Int, weight w: Int, height h: Int) {
       self.seq = seq
       self.weight = w
       self.height = h
   }
}

func sortTest() {
// Create test data
// Number of people
   let N = 10
   let w = [50, 42, 69, 80, 79, 81, 70, 53, 59, 50]
   let h = [184, 170, 170, 169, 193, 154, 147, 180 ,170, 171]
   let personDate = (0..<N).map {
       Person(seq: $0, weight: w[$0], height: h[$0])
   }
   
print ("____________ before sorting")
   _ = personDate.map {
print ("Identification number \ ($ 0.seq), Height: \ ($ 0.height), Weight: \ ($ 0.weight)")
   }
   
// First condition: in ascending order of height
// Second condition: If you are the same height, sort in ascending order of weight
   let sortedPersonDate = personDate.sorted {
       if $0.height != $1.height { return $0.height < $1.height }
       else {
           return $0.weight < $1.weight
       }
   }
   
print ("__________________ After sorting")
   _ = sortedPersonDate.map {
print ("Identification number \ ($ 0.seq), Height: \ ($ 0.height), Weight: \ ($ 0.weight)")
   }
}

____________________ Before sorting
Identification number 0, Height: 184, Weight: 50
Identification number 1, height: 170, weight: 42
Identification number 2, height: 170, weight: 69
Identification number 3, height: 169, weight: 80
Identification number 4, height: 193, weight: 79
Identification number 5, Height: 154, Weight: 81
Identification number 6, height: 147, weight: 70
Identification number 7, height: 180, weight: 53
Identification number 8, height: 170, weight: 59
Identification number 9, Height: 171, Weight: 50
____________________ After sorting
Identification number 6, height: 147, weight: 70
Identification number 5, Height: 154, Weight: 81
Identification number 3, height: 169, weight: 80
Identification number 1, height: 170, weight: 42
Identification number 8, height: 170, weight: 59
Identification number 2, height: 170, weight: 69
Identification number 9, Height: 171, Weight: 50
Identification number 7, height: 180, weight: 53
Identification number 0, Height: 184, Weight: 50
Identification number 4, height: 193, weight: 79

Recommended Posts

Specify multiple sort conditions in Swift
[swift5] How to specify color in hexadecimal
Sort by multiple conditions using Java Stream
Sort by multiple fields in the class
[Xcode 12] [Swift] Recommended coding style when judging multiple conditions in the guard clause
[java] sort in list
Compare objects in Swift
Division becomes 0 in Swift
Multidimensional array in Swift
[Neta] Sleep Sort in Java
Photo library in Swift UI
Implement Swift UITextField in code
Use multiple checkboxes in Rails6!
Specify mysql socket in Hanami
Swift: A trap when setting multiple initial elements in an array