Advertisement
Larme

Untitled

Jan 14th, 2021
908
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.34 KB | None | 0 0
  1. func sections() {
  2.     struct LineRow {
  3.         let lineName: String?
  4.         let lineId: String?
  5.         let nodeName: String
  6.     }
  7.  
  8.     let rows: [LineRow] = [LineRow(lineName: "LINE1", lineId: "1", nodeName: "NODE1-1"),
  9.                            LineRow(lineName: "LINE1", lineId: "1", nodeName: "NODE1-2"),
  10.                            LineRow(lineName: "LINE1", lineId: "1", nodeName: "NODE1-3"),
  11.                            LineRow(lineName: "LINE2", lineId: "2", nodeName: "NODE2-1"),
  12.                            LineRow(lineName: "LINE2", lineId: "2", nodeName: "NODE2-2"),
  13.                            LineRow(lineName: "LINE3", lineId: "3", nodeName: "NODE3-1")]
  14.  
  15.     struct Sections {
  16.         let lineId: String?
  17.         let lineName: String?
  18.         let nodes: [LineRow]
  19.     }
  20.  
  21.     let sections = Dictionary(grouping: rows, by: { $0.lineId }).values.compactMap { Sections(lineId: $0.first?.lineId, lineName: $0.first?.lineName, nodes: $0) }.sorted(by: { $0.lineId ?? "" < $1.lineId ?? "" })
  22.     sections.forEach { aSection in
  23.         print("aSection: \(aSection.lineId)")
  24.         aSection.nodes.forEach { print("\t \($0.nodeName)")}
  25.     }
  26.     //NumberOfSection: sections.count
  27.     //NUmberOfRowsInSection: sections[section].nodes.count
  28.     //Element at IndexPath: let person = sections[indexPath.section].nodes[indexPath.row]
  29. }
  30.  
  31. sections()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement