2016-12-19 56 views
5

đối tượng của tôi trông giống như:sắp xếp theo hai thuộc tính

object = [[section: "section1", order: "1"], 
      [section: "section2", order: "1"], 
      [section: "section1", order: "2"], 
      [section: "section2", order: "2"]] 

tôi muốn sắp xếp nó để có một kết quả như:

[[section: "section1", order: "1"], 
[section: "section1", order: "2"], 
[section: "section2", order: "1"], 
[section: "section2", order: "2"]] 

Vì vậy, tôi cần phải sắp xếp theo phần, và trong mỗi phần bằng gọi món.

Đó là những gì tôi đang làm:

return Observable.from(realm 
      .objects(Section.self).sorted(byProperty: "order", ascending: true) 

Chuỗi "phần .." chỉ là ví dụ, nó có thể được điều khác vì vậy tôi không thể chỉ sử dụng ký tự để sắp xếp. Tôi cần một ưu tiên thực sự trên X chuỗi.

+2

có thể trùng lặp của [Swift - Sắp xếp mảng các đối tượng với nhiều tiêu chí] (http://stackoverflow.com/questions/37603960/swift-sort-array-of-objects-with-multiple-criteria) – Hamish

+0

Có vẻ như bạn đang sử dụng Realm. Bạn nên lưu ý rõ hơn trong câu hỏi của mình. Bạn có thể sắp xếp kết quả sau khi tạo một mảng Swift như được hiển thị trong một số câu trả lời, nhưng bạn có thể sử dụng [phương thức 'sắp xếp' khác] (https://realm.io/docs/swift/latest/api/Classes/Results.html#/s: FC10RealmSwift7Results6sorteduRd__s8SequenceWd__8Iterator7Element_zVS_14SortDescriptorrFT2byqd ___ GS0_x_) của lớp 'Kết quả' của Vương quốc. có thể lấy nhiều 'SortDescriptor'. – OOPer

Trả lời

5

Để sắp xếp nó bởi hai yếu tố bạn có thể làm logic tùy chỉnh của bạn với các phương pháp "sắp xếp": Dưới đây là một ví dụ mà bạn có thể kiểm tra trong một sân chơi.

struct MyStruct { 
    let section: String 
    let order: String 
} 
let array = [MyStruct(section: "section1", order: "1"), 
      MyStruct(section: "section2", order: "1"), 
      MyStruct(section: "section1", order: "2"), 
      MyStruct(section: "section2", order: "2")] 

let sortedArray = array.sorted { (struct1, struct2) -> Bool in 
    if (struct1.section != struct2.section) { // if it's not the same section sort by section 
     return struct1.section < struct2.section 
    } else { // if it the same section sort by order. 
     return struct1.order < struct2.order 
    } 
} 
print(sortedArray) 
+0

Điều này thật mạnh mẽ, cảm ơn bạn! – Makaille

0
let object = [["section": "section1", "order": "2"], 
       ["section": "section2", "order": "2"], 
       ["section": "section1", "order": "1"], 
       ["section": "section2", "order": "1"], 
       ["section": "section5", "order": "3"], 
       ["section": "section6", "order": "1"], 
       ["section": "section5", "order": "1"]] 

    let Ordered = object.sorted{$0["order"]! < $1["order"]! } 

    let OrderedObjects = Ordered.sorted{$0["section"]! < $1["section"]! } 


    print(OrderedObjects) 

//[["section": "section1", "order": "1"], ["section": "section1", "order": "2"], ["section": "section2", "order": "1"], ["section": "section2", "order": "2"], ["section": "section5", "order": "1"], ["section": "section5", "order": "3"], ["section": "section6", "order": "1"]] 
Các vấn đề liên quan