0

I make a json call to get some data from as seen from the image, but I have to sort these items by stargazers_count, ie the items with the largest stargazers_count put before.

Can you give me a hand?

enter image description here

Code:

import SwiftUI
import AppKit

struct Obj: Codable, Identifiable {
    public var id: Int
    public var name: String
    public var language: String?
    public var description: String?
    public var stargazers_count: Int
    public var forks_count: Int
}

class Fetch: ObservableObject {
    @Published var results = [Obj]()
    init(name: String) {
        let url = URL(string: "https://api.github.com/users/"+name+"/repos?per_page=1000")!
        URLSession.shared.dataTask(with: url) { data, response, error in
            do {
                if let data = data {
                    let results = try JSONDecoder().decode([Obj].self, from: data)
                    DispatchQueue.main.async {
                        self.results = results
                    }
                    print("Ok.")
                } else {
                    print("No data.")
                }
            } catch {
                print("Error:", error)
            }
        }.resume()
    }
}

struct ContentViewBar: View {
    @ObservedObject var fetch = Fetch(name: "github")
    var body: some View {
        VStack(alignment: .leading, spacing: 0) {
            List(fetch.results) { el in
                VStack(alignment: .leading, spacing: 0) {
                    Text("\(el.name) (\(el.stargazers_count)/\(el.forks_count)) \(el.language ?? "")").padding(EdgeInsets(top: 5, leading: 0, bottom: 0, trailing: 0))
                    if el.description != nil {
                        Text("\(el.description ?? "")")
                            .font(.system(size: 11))
                            .foregroundColor(Color.gray)
                    }
                }
            }.listStyle(SidebarListStyle())
            
            //            Button(action: { NSApplication.shared.terminate(self) }){
            //                Text("X")
            //                .font(.caption)
            //                .fontWeight(.semibold)
            //            }
            //            .padding(EdgeInsets(top: 2, leading: 0, bottom: 2, trailing: 2))
            //            .frame(width: 360.0, alignment: .trailing)
        }
        .padding(0)
        .frame(width: 360.0, height: 360.0, alignment: .top)
    }
}


struct ContentViewBar_Previews: PreviewProvider {
    static var previews: some View {
        ContentViewBar()
    }
}
Paul
  • 3,644
  • 9
  • 47
  • 113
  • 1
    Does this answer your question? [Swift how to sort array of custom objects by property value](https://stackoverflow.com/questions/24130026/swift-how-to-sort-array-of-custom-objects-by-property-value) – Joakim Danielson Aug 25 '20 at 19:31

2 Answers2

0

In the end, he seems to have succeeded.

Code:

List(fetch.results.sorted { $0.stargazers_count > $1.stargazers_count }) { el in
Paul
  • 3,644
  • 9
  • 47
  • 113
0

Sorting in UI drawing cycle might result in performance issue (especially in case of huge container), so it is better to perform sort either in-advance, or out-of-UI flow.

So I'd recommend to do it here

let results = try JSONDecoder().decode([Obj].self, from: data)
let sorted = results.sorted { $0.stargazers_count > $1.stargazers_count }
DispatchQueue.main.async {
    self.results = sorted
}
Asperi
  • 228,894
  • 20
  • 464
  • 690